FileUplaod : A required anti-forgery token was not supplied or was invalid
I'm trying to do an upload file in my ASP.NET MVC 2 web application but I got an error. A required anti-forgery token was not supplied or was invalid.
There is my aspx code :
<% using (Html.BeginForm("ImportFile", "Suivi", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<input type="file" id="fileUpload" name="fileUpload" />
<input type="submit" value="Import" />
<% } %>
And there is my method in my controller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ImportFile(HttpPostedFileBase fileUpload)
{
if( fileUpload == null)
{
开发者_JS百科 //Process files
}
return View();
}
And there is the error stack :
A required anti-forgery token was not supplied or was invalid.
at System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext filterContext)
at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
So where is my problem ?
Cheers
Skilpit
Your controller action is decorated with the [ValidateAntiForgeryToken]
attribute meaning that it will try to validate the token. So you need to include this token inside the form using the Html.AntiForgeryToken
helper:
<% using (Html.BeginForm("ImportFile", "Suivi", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<%= Html.AntiForgeryToken() %>
<input type="file" id="fileUpload" name="fileUpload" />
<input type="submit" value="Import" />
<% } %>
精彩评论