Sending File in Chunks to HttpHandler

前端 未结 2 1128
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 21:10

I\'m trying to send a file in chunks to an HttpHandler but when I receive the request in the HttpContext, the inputStream is empty.

So a: while sending I\'m not sur

2条回答
  •  萌比男神i
    2021-01-05 21:21

    i was trying the same idea and was successfull in sending file through Post httpwebrequest. Please see the following code sample

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

    I have blogged about it, you see the whole HttpHandler/HttpWebRequest post here http://aspilham.blogspot.com/2011/03/file-uploading-in-chunks-using.html I hope this will help

提交回复
热议问题