将块中的文件发送到HttpHandler

tee*_*bot 5 .net c# httphandler filestream

我正在尝试将块中的文件发送到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();
    }
Run Code Online (Sandbox Code Playgroud)

这就是我在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);
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 2

我怀疑您将其作为表单编码上传可能会令人困惑。但这不是您要发送的内容(除非您掩盖了某些内容)。MIME 类型真的正确吗?

数据有多大?您需要分块上传吗?有些服务器可能不喜欢在单个请求中这样做;我很想通过WebClient.UploadData.