5

ファイルをチャンクで 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);
    }
4

2 に答える 2

2

フォームエンコードしてアップロードしていると混乱するかもしれません。しかし、それはあなたが送信しているものではありません (何かを詳しく説明している場合を除きます)。その MIME タイプは本当に正しいですか?

データの大きさは?チャンクアップロードが必要ですか? 一部のサーバーは、単一の要求でこれを好まない場合があります。経由で複数の単純なリクエストを使用したくなるでしょうWebClient.UploadData

于 2009-11-06T10:02:28.707 に答える
0

私は同じアイデアを試していて、Posthttpwebrequestを介してファイルを送信することに成功しました。次のコードサンプルを参照してください

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

私はそれについてブログを書きました、あなたはここにHttpHandler/HttpWebRequestの投稿全体を見るでしょ うhttp://aspilham.blogspot.com/2011/03/file-uploading-in-chunks-using.html これが役立つことを願っています

于 2011-03-05T12:15:35.677 に答える