我正在尝试将文件分块发送到 HttpHandler 但是当我在 HttpContext 中收到请求时,inputStream 为空。
所以a:发送时我不确定我的HttpWebRequest是否有效,b:接收时我不确定如何在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);
}