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

时间:2015-11-25 15:59:01

标签: c# asp.net asp.net-mvc wcf out-of-memory

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

2 个答案:

答案 0 :(得分:1)

问题不在我想的代码中。 ASP.NET项目托管在IIS Express而不是本地IIS中。因为我在项目属性中改变了一切,所以一切顺利。

我现在正在使用@nimeshjm的代码。谢谢你的帮助!

答案 1 :(得分:0)

您可以尝试使用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("");
    }