2011-01-14 68 views
0

我想從我的ASP.NET MVC web應用程序使用uploadify做簡單的文件上傳。在IE8中,它工作正常。在Firefox和Chrome中,它似乎永遠不會發布到控制器操作。有人能幫我找到我做錯了什麼嗎?爲什麼不在Firefox或Chrome中爲我上傳工作?

這裏是我的html:

<input type="file" id="file_upload" name="FileData" /> 

我包括jQuery的1.4.1的最新版本2.1.4 uploadify,它本身包含的SWFObject 2.2的內容。

這裏是我的腳本:

$(函數(){

$("#file_upload").uploadify({ 
    'uploader': '/Scripts/uploadify.swf', 
    'script':  '/Uploads/UploadFile', 
    'cancelImg': '/Content/Images/cancel.png', 
    'auto':  true, 
    'multi':  false, 
    'folder':  '/uploads', 

    onComplete : function() { 
    alert("complete"); 
    }, 

    onOpen : function() { 
    alert("open"); 
    }, 

    onError : function (event, id, fileObj, errorObj) { 
    alert("error: " + errorObj.info); 
    } 

}); 

});

這是我的控制器操作:

public string UploadFile(HttpPostedFileBase FileData) 
{ 
    // do stuff with the file 
} 

在Chrome和Firefox,我得到一個「錯誤#2038」的字樣,這似乎從我可以找到關於谷歌的相當神祕。我究竟做錯了什麼?

+0

當和你在哪裏得到的錯誤訊息? – 2011-01-14 17:56:37

+0

錯誤消息僅在onError回調中的errorObj中。 – 2011-01-14 20:17:08

回答

3

事情嘗試:

  1. 你的控制器動作應返回的ActionResult,不串
  2. 安裝Fiddler,看到在幕後發生了什麼(你會看到HTTP請求/響應幀和可能的錯誤) 。然後比較不同瀏覽器之間的結果,看看是否有變化。
0

像克里斯農民表示,會議是在閃存請求,餅乾.ASPXAUTH(或其他會話cookie)都無法在Chrome和Firefox發送不同的(你可以用Fiddler2看這個)

爲了解決這個問題,你可以使用uploadify的「scriptData」。這是我如何着手:

添加到您的uploadify JS:

string scriptDataValues = string.Empty; 
      if (Request.Cookies != null && Request.Cookies.Length > 0) 
      { 
       // Generate scriptData 
       scriptDataValues = ", 'scriptData' : {"; 
       string[] formatedData = new string[Request.Cookies.Length]; 
       int i = 0; 
       foreach (HttpCookie cookie in cookies) 
       { 
        // Format cookie to scriptData name:value 
        formatedData[i] = string.Format("\"{0}\":\"{1}\"", cookie.Name, cookie.Value); 
        i++; 
       } 
       // separate all formated cookies with comma 
       scriptDataValues += string.Join(",", formatedData); 
       scriptDataValues += "}"; 
      } 
    // add scriptData to your js script 
    string yourScript = "<script type=\"text/javascript\"> 
$(document).ready(function() { $('#file_upload').uploadify({ 
     'uploader' : '/uploadify/uploadify.swf', 
     'script'  : '/uploadify/uploadify.php', 
     'cancelImg' : '/uploadify/cancel.png', 
     'folder'  : '/uploads' 
     " + scriptDataValues + " 
    }); }); 
</script>" 

,並在你的控制你的行動:

[HttpPost] 
     public ActionResult UploadProductImage(HttpPostedFileBase image, FormCollection collec) 
     { 
      Partner partner = null; 
      if (!string.IsNullOrEmpty(collec[".ASPXAUTH"])) 
      { 
       // Get cookie in POST values 
       FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(collec[".ASPXAUTH"]); 
       if (ticket.Expiration > DateTime.Now) 
       { 
        // Authenticated user, upload the file and return url 
       } 
      } 
     return this.Content(string.Empty); 
     } 
相關問題