【发布时间】:2015-11-25 15:59:00
【问题描述】:
我想通过 ASP.NET 将大文件上传到 WCF 服务。在 100 MB 没有问题之前,我的配置可以完美运行,但超过 100 MB 时会引发 System.OutOfMemoryException。
上传方法适用于 FileStream,但在此之前,我将文件保存到临时文件夹。不确定这是问题还是其他问题。我添加了我的控制器的代码,它负责调用 wcf 服务。
[HttpPost]
public ActionResult Upload()
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Images"), fileName);
file.SaveAs(path);
FileStream fsSource = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
TileService.TileServiceClient client = new TileService.TileServiceClient();
client.Open();
client.UploadFile(fileName, fsSource);
client.Close();
fsSource.Dispose();
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
}
}
return RedirectToAction("");
}
方法是这样调用的:
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="FileUploader" />
<br />
<input type="submit" name="Submit" id="Submit" value="Upload file" />
}
在 ASP.NET web.config 中,我已经设置了以下内容:executionTimeout、maxRequestLength、requestLengthDiskThreshold、maxAllowedContentLength。我添加了配置的绑定部分。
<basicHttpBinding>
<binding name="BasicHttpBinding_ITileService"
closeTimeout="24:01:00" openTimeout="24:01:00" receiveTimeout="24:10:00" sendTimeout="24:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="4294967295" maxBufferSize="2147483647" maxReceivedMessageSize="4294967295" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true" messageEncoding="Text">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
【问题讨论】:
-
您可以尝试使用 Request.Files[0].InputStream 分块读取它
标签: c# asp.net asp.net-mvc wcf out-of-memory