2010-09-24 69 views
8

我想让Uploadify与我的网站一起工作,但即使在文件发送到服务器之前我也会收到一个通用的“HTTP错误”(我之所以这样说是因为Fiddler会这样做不显示任何邮寄请求到我的控制器获取Uploadify与asp.net-mvc一起工作

我可以正确浏览上传的文件队列正确地填充上传的文件,但是当我点击提交按钮时,队列中的元素会变成红色说HTTP错误

反正这是我的部分代码:

<% using (Html.BeginForm("Upload", "Document", FormMethod.Post, new { enctype = "multipart/form-data" })) { %> 
<link type="text/css" rel="Stylesheet" media="screen" href="/_assets/css/uploadify/uploadify.css" /> 
<script type="text/javascript" src="/_assets/js/uploadify/swfobject.js"></script> 
<script type="text/javascript" src="/_assets/js/uploadify/jquery.uploadify.v2.1.0.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     $("[ID$=uploadTabs]").tabs(); 

     var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>"; 
     $('#fileInput').uploadify({ 
      uploader: '/_assets/swf/uploadify.swf', 
      script: '/Document/Upload', 
      folder: '/_uploads', 
      cancelImg: '/_assets/images/cancel.png', 
      auto: false, 
      multi: false, 
      scriptData: { token: auth }, 
      fileDesc: 'Any document type', 
      fileExt: '*.doc;*.docx;*.xls;*.xlsx;*.pdf', 
      sizeLimit: 5000000, 
      scriptAccess: 'always', //testing locally. comment before deploy 
      buttonText: 'Browse...' 
     }); 

     $("#btnSave").button().click(function(event) { 
      event.preventDefault(); 
      $('#fileInput').uploadifyUpload(); 
     }); 

    }); 
</script> 
    <div id="uploadTabs"> 
     <ul> 
      <li><a href="#u-tabs-1">Upload file</a></li> 
     </ul> 
     <div id="u-tabs-1"> 
      <div> 
      <input id="fileInput" name="fileInput" type="file" /> 
      </div> 
      <div style="text-align:right;padding:20px 0px 0px 0px;"> 
       <input type="submit" id="btnSave" value="Upload file" /> 
      </div> 
     </div> 
    </div> 
<% } %> 

非常感谢您的帮助!

UPDATE

我已经加入了“的onError”处理器的uploadify脚本来探讨哪些错误是怎么回事如下面的示例

onError: function(event, queueID, fileObj, errorObj) { 
    alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]"); 
} 

中,发现该信息属性包含。我还添加了“方法”参数以上传至值为'帖子'

我包括我的控制器操作代码的信息。我已经看了很多帖子关于uloadify,似乎我可以使用具有以下签名的动作......

[HttpPost] 
public ActionResult Upload(string token, HttpPostedFileBase fileData) { 
    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token); 
    if (ticket!=null) { 
     var identity = new FormsIdentity(ticket); 
     if(identity.IsAuthenticated) { 
      try { 
       //Save file and other code removed 
       return Content("File uploaded successfully!"); 
      } 
      catch (Exception ex) { 
       return Content("Error uploading file: " + ex.Message); 
      } 
     } 
    } 
    throw new InvalidOperationException("The user is not authenticated."); 
} 

任何人能提供一些帮助吗?

+0

没有机会得到这方面的帮助?:( – Lorenzo 2010-09-27 18:33:59

回答

7

干得好,问题没有了!

我的代码没有“正常”的问题。插件的使用通常是正确的,但是认证机制存在问题。

正如大家可以在互联网上找到的那样,flash插件不会与服务器端代码共享认证cookie,这就是在我的代码中使用包含Authentication Cookie的“scriptData”部分背后的原因。

该问题与控制器装有[Authorize]属性并且从未让请求到达其目的地有关。

该解决方案在uploadify论坛上的其他用户的帮助下找到,就是编写一个自定义版本的AuthorizeAttribute,就像您在以下代码中看到的一样。

/// <summary> 
/// A custom version of the <see cref="AuthorizeAttribute"/> that supports working 
/// around a cookie/session bug in Flash. 
/// </summary> 
/// <remarks> 
/// Details of the bug and workaround can be found on this blog: 
/// http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx 
/// </remarks> 
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] 
public class TokenizedAuthorizeAttribute : AuthorizeAttribute 
{ 
    /// <summary> 
    /// The key to the authentication token that should be submitted somewhere in the request. 
    /// </summary> 
    private const string TOKEN_KEY = "AuthenticationToken"; 

    /// <summary> 
    /// This changes the behavior of AuthorizeCore so that it will only authorize 
    /// users if a valid token is submitted with the request. 
    /// </summary> 
    /// <param name="httpContext"></param> 
    /// <returns></returns> 
    protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) { 
     string token = httpContext.Request.Params[TOKEN_KEY]; 

     if (token != null) { 
      FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token); 

      if (ticket != null) { 
       FormsIdentity identity = new FormsIdentity(ticket); 
       string[] roles = System.Web.Security.Roles.GetRolesForUser(identity.Name); 
       GenericPrincipal principal = new GenericPrincipal(identity, roles); 
       httpContext.User = principal; 
      } 
     } 

     return base.AuthorizeCore(httpContext); 
    } 
} 

用它来装饰控制器/上传的动作使所有的工作都能顺利进行。

仍然未解决的唯一奇怪的事情,但不影响代码的执行,奇怪的是,Fiddler不显示HTTP帖子。我不明白为什么....

我发布此信息使其可用于社区。

谢谢!

+0

那么这是否意味着未经授权的用户可以上传文件? – 2010-12-01 22:22:34

+0

是的。如果你不使用这样的代码。 – Lorenzo 2010-12-01 23:05:06

+2

Fiddler没有显示的帖子是因为你需要使用ipv4.fiddler作为域而不是localhost。 – 2011-02-03 21:37:28