2009-11-06 9 views
5

Próbuję wysłać plik w porcjach do HttpHandler, ale gdy otrzymam żądanie w HttpContext, inputStream jest pusty.Wysyłanie pliku w porcjach do HttpHandler

Więc: podczas wysyłania Nie jestem pewien, czy mój HttpWebRequest jest ważny i b: podczas odbioru nie jestem pewien, w jaki sposób odzyskać strumień w HTTPContext

Każda pomoc bardzo mile widziane!

ten sposób mam złożyć wniosek kodu klient:

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

ten sposób obsłużyć żądania w 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

Co CopyStream zrobić? Spróbuj użyć StreamReadera i odczytaj z niego bajty context.Request.TotalBytes. – configurator

Odpowiedz

3

Podejrzewam, że może być mylące, że przesyłasz je jako zakodowane w formie. ale to nie jest to, co wysyłasz (chyba, że ​​coś glosujesz). Czy ten typ MIME jest naprawdę poprawny?

Jak duże są dane? Czy potrzebujesz ładowanego pliku? Niektóre serwery mogą tego nie lubić w jednym żądaniu; Byłbym skłonny użyć wielu prostych żądań za pośrednictwem WebClient.UploadData.

+0

Wysyłam plik. Dane mogą być duże, dlatego chcę je przesłać w porcjach. również kod klienta musi znać status transferu (dla paska ładowania) – teebot

+0

Yup WebClient.UploadData załatwił sprawę! Dzięki Marc Gravell! – teebot

0

Próbowałem tego samego pomysłu i udało mi się wysłać plik poprzez Post httpwebrequest. Zobacz następujący przykładowy kod

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

mam blogu o tym, widzisz cały HttpHandler/HttpWebRequest post tutaj http://aspilham.blogspot.com/2011/03/file-uploading-in-chunks-using.html Mam nadzieję, że to pomoże

Powiązane problemy