2012-03-06 11 views
6

Próbuję debugować asynchroniczny program do przesyłania plików, który zbudowałem jakiś czas temu, który już nie działa, spędziłem już dużo czasu bez powodzenia.Przesyłanie asynchronicznych plików HTML5, przesłany strumień jest zawsze nieprawidłowy

Strumień odbierany przez serwer jest zawsze uszkodzony, w rzeczywistości zapisany plik (obraz) nie może zostać otwarty.

Aby uprościć debugowanie, skonfigurowałem zupełnie nowy projekt ASP.NET, z dwoma głównymi plikami, plikiem HTML z polem formularza i obsługą programu ASP.NET.

Pomimo tego, że kod jest tu bardzo banalny, wciąż mam pecha! :(

Każda pomoc jest bardzo ceniona, wiele dzięki

<!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> 

i tutaj jest handler 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

To jest to, co Ctrl + K + D robi w VS, nie spędzam zbyt wiele czasu, aby go sformatować szczerze! –

+2

@llnk nowa linia dla C# jest dość standardowa. Zawsze uczono mnie, aby nie pisać kodu "Z akcentem", a Giuseppe faktycznie przestrzega tutaj dość standardowych konwencji kodowych. – Crisfole

Odpowiedz

5

W przypadku pomogło to ktoś inny, mam kod do praca, tutaj jest, co to jest zmienione:

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

Plus 1 dla kodu po stronie klienta! Tego nie mogłem dostać do pracy. – sreimer

Powiązane problemy