views:

374

answers:

1

Hello,

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);
    }
+1  A: 

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.

Marc Gravell
I'm sending a file. The data can be big that's why I want to send it in chunks. also client code needs to know the status of the transfer (for a loading bar)
teebot.be
Yup WebClient.UploadData did the trick! Thanks Marc Gravell!
teebot.be