Sending File in Chunks to HttpHandler

前端 未结 2 1111
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  • 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

    0 讨论(0)
  • 2021-01-05 21:39

    I suspect it might be confusing that you are uploading it as form-encoded. but that isn't what you are sending (unless you're glossing over something). Is that MIME type really correct?

    How big is the data? Do you need chunked upload? Some servers might not like this in a single request; I'd be tempted to use multiple simple requests via WebClient.UploadData.

    0 讨论(0)
提交回复
热议问题