【问题标题】:Transfer really large files using WebClient使用 WebClient 传输非常大的文件
【发布时间】:2014-05-27 10:57:14
【问题描述】:

我的要求是传输大小为 400MB 或更大的 zip 文件;以下代码至少适用于 40MB;但要获得更多,我必须将 byte[] bytes = new byte[50000000]; 更改为 byte[] bytes = new byte[400000000]; 并将 maxRequestLength 更改为 maxRequestLength="409600";

问题是byte[] bytes = new byte[100000000]; 返回有关空间不足的错误。那么如何使用 WebClient 传输大文件呢?

WebClient client = new WebClient();
client.AllowWriteStreamBuffering = true;
UriBuilder ub = new UriBuilder("http://localhost:57596/UploadImages.ashx");
ub.Query = "ImageName=" + "DataSet" + DataSetId + ".zip";
client.OpenWriteCompleted += (InputStream, eArguments) =>
{

    try
    {
        using (Stream output = eArguments.Result)
        {

            output.Write(ImagesAux, 0, (int)ImagesAux.Length);
            //numeroimagem++;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        //throw;
    }

};
client.OpenWriteAsync(ub.Uri);

在 UploadImages.ashx 中

public void ProcessRequest(HttpContext context)
{
    //context.Response.ContentType = "text/plain";
    //context.Response.Write("Hello World");

    string ImageName = context.Request.QueryString["ImageName"];
    string UploadPath = context.Server.MapPath("~/ServerImages/");

    using (FileStream stream = File.Create(UploadPath + ImageName))
    {

        byte[] bytes = new byte[50000000]; // 
        int bytesToRead = 0;

        while ((bytesToRead =
        context.Request.InputStream.Read(bytes, 0, bytes.Length)) != 0)
        {

            stream.Write(bytes, 0, bytesToRead);
            stream.Close();

        }

    }

}

在 Web.config 中

<httpRuntime targetFramework="4.5" maxRequestLength="40960"/>

【问题讨论】:

  • bytes[500000] 只是我会保持较低的缓冲区大小,不要在每次写入时关闭流。

标签: c# silverlight webclient


【解决方案1】:

您永远不应该将所有内容加载到内存中然后将所有内容写回磁盘,而是应该在阅读时加载片段并将它们写入磁盘。 完成阅读后,您将关闭正在写入的流。 否则,一旦达到 GB 大小,您就可以很快获得 OutOfMemory。

所以我会从这里将写入字节更改为磁盘:

using (FileStream stream = File.Create(UploadPath + ImageName))
{
    byte[] bytes = new byte[50000000]; // 
    int bytesToRead = 0;
    while ((bytesToRead = context.Request.InputStream.Read(bytes, 0, bytes.Length)) != 0)
    {
        stream.Write(bytes, 0, bytesToRead);
        stream.Close();
    }
}

对此:

using (FileStream stream = File.Create(UploadPath + ImageName))
{
    byte[] bytes = new byte[1024];
    long totalBytes = context.Request.InputStream.Length;
    long bytesRead = 0;
    int bytesToRead = bytes.Length;

    if (totalBytes - bytesRead < bytes.Length)
        bytesToRead = (int)(totalBytes - bytesRead);
    bytes = new byte[bytesToRead];

    while ((bytesToRead = context.Request.InputStream.Read(bytes, bytesRead, bytes.Length)) != 0)
    {
        stream.Write(bytes, bytesRead, bytes.Length);

        bytesRead += bytes.Length;
        if (totalBytes - bytesRead < bytes.Length)
            bytesToRead = (int)(totalBytes - bytesRead);
        bytes = new byte[bytesToRead];

    }
    stream.Close();
}

1024 将是缓冲区大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2021-02-05
    • 2012-06-13
    相关资源
    最近更新 更多