2011-02-03 21 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"); 
}