English 中文(简体)
前往HttpHandler
原标题:Sending File in Chunks to HttpHandler
  • 时间:2009-11-06 09:51:20
  •  标签:

我试图将一个chu子的档案寄给一名HttpHandler,但在我收到HttpContext的请求时,投入Stream是空的。

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

任何帮助都受到高度赞赏。

我如何要求客户守则:

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

这是我如何处理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);
    }
最佳回答

我怀疑这可能会混淆,即你在装上表格。 但是,这等于是你所寄出的东西(除非你重复一些东西)。 该系统的类型是否真的正确?

数据是多少? 您是否需要chu高压? 一些服务器可能无法在单一要求中这样做;我被诱惑通过<代码>WebClient使用多种简单要求。 UploadData.

问题回答

i 正在尝试同样的想法,并且通过网站http://webrequest成功发送档案。 请参看以下代码样本

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





相关问题
热门标签