2012-03-06 80 views
6

我試圖調試一段時間以前建立的不再工作的異步文件上傳器,我已經花了很多時間沒有成功。HTML5異步文件上傳,上傳的流總是無效

服務器正在接收的流總是被損壞,實際上我保存的文件(圖像)無法打開。

爲了簡化調試,我建立了一個全新的ASP.NET項目,其中包含兩個主要文件,帶有表單字段的HTML文件和ASP.NET處理程序。

儘管這裏的代碼非常微不足道,但我依然運氣不佳! :(

任何幫助,高度讚賞,非常感謝

<!DOCTYPE html> 
<html> 
<head> 
    <title>Upload Files using XMLHttpRequest - Minimal</title> 

    <script type="text/javascript"> 

     function uploadFile() { 
     var fd = new FormData(); 
     fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]); 
     var xhr = new XMLHttpRequest(); 

     xhr.addEventListener("load", uploadComplete, false); 
     xhr.addEventListener("error", uploadFailed, false); 
     xhr.addEventListener("abort", uploadCanceled, false); 
     xhr.open("POST", "Handler1.ashx"); 
     xhr.send(fd); 
     } 

     function uploadComplete(evt) { 
     /* This event is raised when the server send back a response */ 
     alert(evt.target.responseText); 
     } 

     function uploadFailed(evt) { 
     alert("There was an error attempting to upload the file."); 
     } 

     function uploadCanceled(evt) { 
     alert("The upload has been canceled by the user or the browser dropped the connection."); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" enctype="multipart/form-data" method="post" action="Handler1.ashx"> 

     <input type="file" name="fileToUpload" id="fileToUpload"/> 
     <input type="button" onclick="uploadFile()" value="Upload" /> 

    </form> 
</body> 
</html> 

這裏是ashx的處理程序:

using System; 
using System.Collections.Generic; 
using System.Web.Extensions; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.IO; 
namespace MultipleFileUploadTest 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    public class Handler1 : IHttpHandler 
    { 
     public void ProcessRequest(HttpContext context) 
     { 
      var stream = context.Request.InputStream; 

      MemoryStream memoryStream; 

      ReadFully(stream, out memoryStream); 

      Byte[] ba = memoryStream.ToArray(); 


      var path = @"C:\Users\giuseppe.JHP\Desktop\Image upload test\uploaded.gif"; 


      using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) 
      { 
       fs.Write(ba, 0, ba.Length); 
      } 

      //DEBUGGING CODE 
      //I'm opening the same file that was originally picked by the input form field and I'm now comparing the original file with the one received within the context stream. They always differ! 
      Byte[] ba2 = File.ReadAllBytes(@"C:\Users\giuseppe.JHP\Desktop\Image upload test\a.gif"); 

      //equal evaluates always to false 
      bool equal = ba.Length == ba2.Length; 

      if (equal) 
      { 
       for (var i = 0; i < ba2.Length; i++) 
       { 
        if (ba[i] != ba2[i]) 
        { 
         equal = false; 
         i = ba2.Length; 
        } 
       } 

      } 

      //equal is always false 
      //if (!equal) 
      //{ 
      // throw Exception("Stream is not valid"); 
      //} 
      //The code below will throw a Parameter is invalid exception 
      //System.Drawing.Image mediaObject = System.Drawing.Image.FromStream(memoryStream); 


      memoryStream.Close(); 
     } 

     public static void ReadFully(Stream input, out MemoryStream ms) 
     { 
      ms = new MemoryStream(); 
      byte[] buffer = new byte[16 * 1024]; 

      int read; 
      while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       ms.Write(buffer, 0, read); 
      } 
     } 
     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 
    } 
} 
+2

這就是Ctrl + K + D在VS中所做的事情,我沒有花太多時間來格式化它,說實話! – 2012-03-06 18:19:06

+2

@llnk C#的新行很標準。我總是被教導不要寫代碼「帶口音」,而Giuseppe實際上是在這裏遵循相當標準的代碼約定。 – Crisfole 2013-01-14 14:56:55

回答

5

如果它幫助別人,我已經得到了代碼工作,這裏是它被改變的地方:

public void ProcessRequest(HttpContext context) 
    { 
     if (context.Request.Files != null && context.Request.Files.Count > 0) 
     { 
      var file = context.Request.Files[0]; 

      file.SaveAs(@"C:\Users\giuseppe.JHP\Desktop\Image upload test\uploaded.gif"); 
     } 
    } 
+1

加1爲客戶端代碼!這是我無法工作的原因。 – sreimer 2012-08-30 21:10:16