2011-02-03 10 views
10

estoy usando Valums Ajax uploader. todo funciona muy bien en Mozilla con este código:MVC Valums Ajax Uploader - IE no envía la secuencia en request.InputStream

Vista:

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 
}); 

controlador:

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 
} 

que fue respondida en este post:

MVC3 Valums Ajax File Upload

Sin embargo, el problema es que esto no funciona en IE. Lo que encontrar esto, pero no puedo encontrar la manera de ponerla en práctica:

IE no envía la corriente en "request.InputStream" ... en lugar de obtener el flujo de entrada a través de la HttpPostedFileBase de los Request.Files [] colección

Además, esto aquí que muestra cómo este hombre lo hizo pero no estoy seguro de cómo cambiar para mi proyecto:

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

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

como HttpPostedFileBase;

no puedo entender esto. ¡por favor ayuda! gracias

Respuesta

16

Me di cuenta. Esto funciona en IE y 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

Sí! Me salvaste alrededor de un millón de horas, gracias. –

+0

Excelente hombre !!!! +10 –

+1

¿Cómo puedo duplicar esto para que funcione en PHP? – dallen

0

La solución de Shane funciona pero parece que la Petición ["qqfile"] se establece de todos modos en IE. No estoy seguro de si esto es porque estoy usando una versión actualizada del archivo de carga pero he modificado la declaración "if" para que funcione para IE (verificando si hay archivos cargados en la solicitud).

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

Aquí es el fragmento completo

[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"); 
} 
Cuestiones relacionadas