简体   繁体   English

使用PushStreamContent从HTTPClient上传

[英]Using PushStreamContent to upload from an HTTPClient

I would like to upload a large amount of data to a web server from a client machine. 我想客户端计算机上将大量数据上传到Web服务器。 I jumped right to PushStreamContent so I could write directly to the stream, as the results vary in size and can be rather large. 我跳到PushStreamContent,所以我可以直接写到流中,因为结果的大小各不相同,而且可能很大。

The flow is as follows: 流程如下:

User runs query > Reader Ready Event Fires > Begin Upload

Once the ready event is fired, the listener picks it up and iterates over the result set, uploading the data as a multipart form: 触发ready事件后,侦听器将其拾取并遍历结果集,以分段形式上传数据:

Console.WriteLine("Query ready, uploading");
        byte[] buffer = new byte[1024], form = new byte[200];
        int offset = 0, byteCount = 0;
        StringBuilder rowBuilder = new StringBuilder();
        string builderS;
        var content = new PushStreamContent(async (stream, httpContent, transportContext) =>
        //using (System.IO.Stream stream = new System.IO.FileStream("test.txt", System.IO.FileMode.OpenOrCreate))
        {
            int bytes = 0;
            string boundary = createFormBoundary();
            httpContent.Headers.Remove("Content-Type");
            httpContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
            await stream.WriteAsync(form, 0, form.Length);
            form = System.Text.Encoding.UTF8.GetBytes(createFormElement(boundary, "file"));
            await stream.WriteAsync(form, 0, form.Length);
            await Task.Run(async () =>
            {
                foreach (var row in rows)
                {
                    for (int i = 0; i < row.Length; i++)
                    {
                        rowBuilder.Append(row[i].Value);
                        if (i + 1 < row.Length)
                            rowBuilder.Append(',');
                        else
                        {
                            rowBuilder.Append("\r\n");
                        }
                    }
                    builderS = rowBuilder.ToString();
                    rowBuilder.Clear();
                    byteCount = System.Text.Encoding.UTF8.GetByteCount(builderS);
                    bytes += byteCount;
                    if (offset + byteCount > buffer.Length)
                    {
                        await stream.WriteAsync(buffer, 0, offset);
                        offset = 0;
                        if (byteCount > buffer.Length)
                        {
                            System.Diagnostics.Debug.WriteLine("Expanding buffer to {0} bytes", byteCount);
                            buffer = new byte[byteCount];
                        }
                    }
                    offset += System.Text.Encoding.UTF8.GetBytes(builderS, 0, builderS.Length, buffer, offset);
                }
            });
            await stream.WriteAsync(buffer, 0, offset);
            form = System.Text.Encoding.UTF8.GetBytes(boundary);
            await stream.WriteAsync(form, 0, form.Length);
            await stream.FlushAsync(); //pretty sure this does nothing
            System.Diagnostics.Debug.WriteLine("Wrote {0}.{1} megabytes of data", bytes / 1000000, bytes % 1000000);

I think the code above would work great if I were the server, just adding stream.Close(); 我认为如果我是服务器,上面的代码会很好用,只需添加stream.Close(); would finish it, however since I am the client here closing it causes an error ( TaskCancelled ). 将完成它,但是由于我是这里的客户端,因此将导致错误( TaskCancelled )。 Waiting to read doesn't do anything either, I presume because the PushStreamContent doesn't end the request unless I explicitly close the stream. 我想等待读取也不会执行任何操作,因为除非我明确关闭流,否则PushStreamContent不会结束请求。 That being said, writing to a file produces exactly what I expect to be uploaded so everything writes perfectly. 话虽如此,写入文件将产生我希望上传的文件,因此一切都能完美写入。

Any ideas on what I can do here? 关于我在这里可以做什么的任何想法? I might be totally misusing PushStreamContent but it seems like this should be an appropriate use case. 我可能完全滥用了PushStreamContent但似乎这应该是一个合适的用例。

So the solution is a little confusing at first but it seems to make sense and perhaps more importantly, it works: 因此,该解决方案起初有点令人困惑,但它似乎很有意义,而且也许更重要的是,它可行:

using(var content = new MultipartFormDataContent()) 
{
  var pushContent = new PushStreamContent(async (stream, httpContent, transportContext) =>
  { 
    //do the stream writing stuff
    stream.Close();
  });
  content.add(pushContent);
  //post, put, etc. content here
}

This works because the stream passed to the PushStreamContent method is not the actual request stream, it's a stream handled by the HttpClient , just like adding a file to a request stream. 之所以PushStreamContent是因为传递给PushStreamContent方法的流不是实际的请求流,而是由HttpClient处理的流,就像将文件添加到请求流一样。 As a result, closing it signals the end of input for this part of the HttpContent and allows the request to be finalized. 结果,关闭它表示HttpContent这部分输入的结束,并允许请求完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM