2011-03-09 36 views
0

我試圖在ASP.NET MVC 2 Web應用程序中執行上載文件,但出現錯誤。 未提供所需的防僞標記或無效。FileUplaod:未提供所需的防僞標記或無效

有我的aspx代碼:

<% using (Html.BeginForm("ImportFile", "Suivi", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ %> 
    <input type="file" id="fileUpload" name="fileUpload" /> 
    <input type="submit" value="Import" /> 
<% } %> 

而且有我的方法在我的控制器:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult ImportFile(HttpPostedFileBase fileUpload) 
    { 
     if(fileUpload == null) 
     { 
      //Process files 
     } 
     return View(); 
    } 

而且有錯誤堆棧:

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) 

那麼,是我的問題 ?

乾杯

Skilpit

回答

1

你的控制器動作裝飾與[ValidateAntiForgeryToken]屬性意味着它將嘗試驗證令牌。因此,您需要在表單內使用Html.AntiForgeryToken幫手來包含此令牌:

<% 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" /> 
<% } %> 
相關問題