Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending File in Chunks to HttpHandler

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 sure if my HttpWebRequest is valid and b: while receiving I'm not sure how to retrieve the stream in the HttpContext

Any help greatly appreciated!

This how I make my request from client code:

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

this is how I handle the request in the 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);
    }
like image 925
teebot Avatar asked Nov 06 '22 18:11

teebot


1 Answers

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.

like image 137
Marc Gravell Avatar answered Nov 14 '22 20:11

Marc Gravell