为什么不在Firefox或Chrome中上传我的工作?

时间:2011-01-14 17:47:15

标签: jquery asp.net-mvc uploadify

我正在尝试使用uploadify从我的ASP.NET MVC Web应用程序上进行简单的文件上传。在IE8中,它工作正常。在Firefox和Chrome中,它似乎永远不会发布到控制器操作。有人能帮我找到我做错的事吗?

这是我的html:

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

我包括jquery 1.4.1和当前版本的uploadify 2.1.4的内容,它本身包含swfobject 2.2。

这是我的剧本:

$(function(){

$("#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”消息,这似乎与我在谷歌上找到的内容相当神秘。我做错了什么?

2 个答案:

答案 0 :(得分:3)

要尝试的事情:

  1. 您的控制器操作应该返回ActionResult,而不是字符串
  2. 安装Fiddler,看看幕后会发生什么(您将看到HTTP请求/响应帧和可能的错误)。然后比较不同浏览器之间的结果,看看是否有变化。

答案 1 :(得分:0)

像Chris Farmer所说,会话在flash请求中有所不同,cookies .ASPXAUTH(或其他会话cookie)不会在Chrome和Firefox中发送(你可以用Fiddler2观看)

要解决此问题,您可以将“scriptData”与uploadify一起使用。这就是我的进展方式:

将此添加到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);
        }