2010-09-24 58 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