2009-11-06 9 views
5

Sto cercando di inviare un file in blocchi a un HttpHandler, ma quando ricevo la richiesta in HttpContext, inputStream è vuoto.Invio di file in blocchi a HttpHandler

Quindi un: durante l'invio non sono sicuro se il mio HttpWebRequest è valido e B: durante la ricezione non sono sicuro di come recuperare il flusso nella HttpContext

Qualsiasi aiuto molto apprezzato!

questo come io faccio la mia richiesta dal codice client:

private void Post(byte[] bytes) 
    { 
     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:2977/Upload"); 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.SendChunked = true; 
     req.Timeout = 400000; 
     req.ContentLength = bytes.Length; 
     req.KeepAlive = true; 

     using (Stream s = req.GetRequestStream()) 
     { 
      s.Write(bytes, 0, bytes.Length); 
      s.Close(); 
     } 

     HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 
    } 

questo è come mi gestire la richiesta nel HttpHandler:

public void ProcessRequest(HttpContext context) 
    { 
     Stream chunk = context.Request.InputStream; //it's empty! 
     FileStream output = new FileStream("C:\\Temp\\myTempFile.tmp", FileMode.Append); 

     //simple method to append each chunk to the temp file 
     CopyStream(chunk, output); 
    } 
+0

Cosa CopyStream fare? Prova a utilizzare uno StreamReader e leggi context.Request.TotalBytes byte da esso. – configurator

risposta

3

ho il sospetto che potrebbe essere fonte di confusione che si sta caricando è come codificato in forma. ma non è quello che stai mandando (a meno che tu non stia sorvogliando qualcosa). Questo tipo di MIME è veramente corretto?

Quanto sono grandi i dati? Hai bisogno di un caricamento chunked? Alcuni server potrebbero non piacere in una singola richiesta; Sarei tentato di utilizzare più richieste semplici tramite WebClient.UploadData.

+0

Sto inviando un file. I dati possono essere grandi, ecco perché voglio inviarlo in blocchi. anche il codice cliente deve conoscere lo stato del trasferimento (per una barra di caricamento) – teebot

+0

Yup WebClient.UploadData ha fatto il trucco! Grazie Marc Gravell! – teebot

0

stavo provando la stessa idea e ho avuto successo nell'invio di file tramite Post httpwebrequest. Si prega di consultare il seguente codice di esempio

private void ChunkRequest(string fileName,byte[] buffer) 


{ 
//Request url, Method=post Length and data. 
string requestURL = "http://localhost:63654/hello.ashx"; 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); 
request.Method = "POST"; 
request.ContentType = "application/x-www-form-urlencoded"; 

// Chunk(buffer) is converted to Base64 string that will be convert to Bytes on the handler. 
string requestParameters = @"fileName=" + fileName + 
"&data=" + HttpUtility.UrlEncode(Convert.ToBase64String(buffer)); 

// finally whole request will be converted to bytes that will be transferred to HttpHandler 
byte[] byteData = Encoding.UTF8.GetBytes(requestParameters); 

request.ContentLength = byteData.Length; 

Stream writer = request.GetRequestStream(); 
writer.Write(byteData, 0, byteData.Length); 
writer.Close(); 
// here we will receive the response from HttpHandler 
StreamReader stIn = new StreamReader(request.GetResponse().GetResponseStream()); 
string strResponse = stIn.ReadToEnd(); 
stIn.Close(); 
} 

ho bloggato su di esso, si vede tutta la HttpHandler/HttpWebRequest post qui http://aspilham.blogspot.com/2011/03/file-uploading-in-chunks-using.html Spero che questo vi aiuterà