【问题标题】:how to send a large file to Request Stream in asp .net如何在asp .net中将大文件发送到请求流
【发布时间】:2012-09-03 14:12:54
【问题描述】:

我想将大于 800 mb 的大文件发送到 cassandra 存储。但我得到了 System.OutOfMemoryException .

请在下面找到代码:

HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

Stream requestStream = request.GetRequestStream();

byte[] buffer = new byte[8 * 1024];

int len;
while ((len = fileToUpload.File.Read(buffer, 0, buffer.Length)) >0)
{
    requestStream.Write(buffer, 0, len);
    requestStream.Flush();
}
return request.GetResponse() as HttpWebResponse;

请建议我哪部分代码需要改进?

fileToUpload 是来自 UI 的 800 MB 的确切流。

【问题讨论】:

  • 你从哪里得到异常?堆栈跟踪是什么?
  • 我在 requestStream.Write(buffer, 0, len);大约 500 MB 完成后

标签: asp.net asp.net-mvc asp.net-mvc-3 wcf rest


【解决方案1】:

默认情况下,使用 HttpRequest 上传的文件的最大大小为 4 MB (4096 KB)。如果您可以访问接收端的代码,您可以在 web.config 中增加它,尽管我从未测试过 800+ MB 文件:) 请记住,Asp.Net 将在 180 秒后超时任何请求,因此实际上限制您可以上传的文件大小。

<httpRuntime
  executionTimeout="110" 
  maxRequestLength="4096" />

Uploading Files in ASP.NET 2.0

恕我直言,您最好的选择是使用 HttpModule 分块发送文件。这些可能很有趣:

Large File Upload Using HttpHandler or HttpModule?

HttpHandler or HttpModule for file upload, large files, progress indicator?

祝你好运:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多