asp.net上传大文件:抛出System.OutOfMemoryException

本文关键字:抛出 System OutOfMemoryException 文件 net asp | 更新日期: 2023-09-27 18:24:15

我想通过ASP.NET将大文件上传到WCF服务。在100MB不是问题之前,我的配置工作得很好,但超过100MB时会抛出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>

asp.net上传大文件:抛出System.OutOfMemoryException

我认为问题不在代码中。ASP.NET项目托管在IIS Express中,而不是本地IIS中。由于我在项目属性中更改了这一点,所以一切都很顺利。

不过,我现在使用的是@nimeshjm的代码。谢谢你的帮助!

您可以尝试使用Request.Files[0].InputStream 分块读取它

大致如下:

    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);
                using (var fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    var buffer = new byte[1024];
                    int count;
                    while ((count = file.InputStream.Read(buffer, 0, 1024)) > 0)
                    {
                        fs.Write(buffer, 0, count);
                    }
                }
                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("");
    }