繁体   English   中英

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

[英]asp.net uploading big files: throws System.OutOfMemoryException

我想通过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>

问题不在于我认为的代码中。 ASP.NET项目托管在IIS Express中,而不是Local 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("");
    }

暂无
暂无

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

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