2011-02-03 22 views
10

我使用Valums Ajax上傳器。所有在Mozilla與此代碼的偉大工程:MVC Valums Ajax Uploader - IE不發送請求流.InputStream

查看:

var button = $('#fileUpload')[0]; 
var uploader = new qq.FileUploader({ 
    element: button, 
    allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
    sizeLimit: 2147483647, // max size 
    action: '/Admin/Home/Upload', 
    multiple: false 
}); 

控制器:

public ActionResult Upload(string qqfile) 
{ 
    var stream = Request.InputStream; 
    var buffer = new byte[stream.Length]; 
    stream.Read(buffer, 0, buffer.Length); 

    var path = Server.MapPath("~/App_Data"); 
    var file = Path.Combine(path, qqfile); 
    File.WriteAllBytes(file, buffer); 

    // TODO: Return whatever the upload control expects as response 
} 

這是在這個崗位回答:

MVC3 Valums Ajax File Upload

但問題是,這在IE中不起作用。我發現這一點,但我無法弄清楚如何實現它:

IE不 「request.InputStream」通過 HttpPostedFileBase從發送流...而不是讓 輸入流在 Request.Files []集合

此外,這在這裏顯示這個傢伙是怎麼做到的,但我不知道如何爲我的項目更改:

Valum file upload - Works in Chrome but not IE, Image img = Image.FromStream(Request.InputStream)

//This works with IE 
HttpPostedFileBase httpPostedFileBase = Request.Files[0] 

as HttpPostedFileBase;

不能找出這一個。請幫忙! 謝謝

回答

16

我想通了。這適用於IE和Mozilla。

[HttpPost] 
     public ActionResult FileUpload(string qqfile) 
     { 
      var path = @"C:\\Temp\\100\\"; 
      var file = string.Empty; 

      try 
      { 
       var stream = Request.InputStream; 
       if (String.IsNullOrEmpty(Request["qqfile"])) 
       { 
        // IE 
        HttpPostedFileBase postedFile = Request.Files[0]; 
        stream = postedFile.InputStream; 
        file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName)); 
       } 
       else 
       { 
        //Webkit, Mozilla 
        file = Path.Combine(path, qqfile); 
       } 

       var buffer = new byte[stream.Length]; 
       stream.Read(buffer, 0, buffer.Length); 
       System.IO.File.WriteAllBytes(file, buffer); 
      } 
      catch (Exception ex) 
      { 
       return Json(new { success = false, message = ex.Message }, "application/json"); 
      } 

      return Json(new { success = true }, "text/html"); 
     } 
+0

呀!你剛剛救了我一百萬小時 - 謝謝。 – 2012-02-01 13:36:37

0

Shane的解決方案工作正常,但似乎在IE中設置了Request [「qqfile」]。不知道這是因爲我正在使用fileuploader的更新版本,但我修改了「if」語句以使其適用於IE(檢查請求中是否有上傳的文件)。

if (Request.Files.Count > 0) { 
    //ie 
} else { 
    //webkit and mozilla 
} 

以下是完整的片段

[HttpPost] 
public ActionResult FileUpload(string qqfile) 
{ 
    var path = @"C:\\Temp\\100\\"; 
    var file = string.Empty; 

    try 
    { 
     var stream = Request.InputStream; 
     if (Request.Files.Count > 0) 
     { 
      // IE 
      HttpPostedFileBase postedFile = Request.Files[0]; 
      stream = postedFile.InputStream; 
      file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName)); 
     } 
     else 
     { 
      //Webkit, Mozilla 
      file = Path.Combine(path, qqfile); 
     } 

     var buffer = new byte[stream.Length]; 
     stream.Read(buffer, 0, buffer.Length); 
     System.IO.File.WriteAllBytes(file, buffer); 
    } 
    catch (Exception ex) 
    { 
     return Json(new { success = false, message = ex.Message }, "application/json"); 
    } 

    return Json(new { success = true }, "text/html"); 
}