2011-02-03 8 views
10

sto usando Valums Ajax uploader. tutte le grandi opere in Mozilla con questo codice:MVC Valums Ajax Uploader - IE non invia lo streaming in 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 
}); 

Controller:

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 
} 

che è stato risposto in questo post:

MVC3 Valums Ajax File Upload

Tuttavia, questo problema non funziona in IE. Ho trovato questo, ma io non riesco a capire come implementarlo:

IE non invia il flusso in "request.InputStream" ... invece ottenere il flusso di input attraverso il HttpPostedFileBase da le Request.Files [] collezione

Inoltre, questo qui che mostra come questo ragazzo ha fatto, ma non sono sicuro come cambiare per il mio progetto:

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

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

come HttpPostedFileBase;

impossibile capirlo. per favore aiuto! grazie

risposta

16

L'ho capito. Funziona in IE e 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

Yeah! mi hai appena salvato circa un milione di ore - grazie. –

+0

Uomo eccellente !!!! +10 –

+1

Come posso duplicare questo per funzionare con PHP? – dallen

0

opere di soluzioni di Shane, ma sembra che la richiesta [ "qqfile"] è stato impostato in ogni caso in IE. Non sono sicuro se questo è perché sto usando una versione aggiornata del fileuploader ma ho modificato l'istruzione "if" per farlo funzionare per IE (controllando se ci sono dei file caricati nella richiesta).

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

Ecco il pieno frammento

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