asp.net上传大文件:抛出的System.OutOfMemoryException [英] asp.net uploading big files: throws System.OutOfMemoryException

查看:315
本文介绍了asp.net上传大文件:抛出的System.OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ASP.NET上传大文件到WCF服务。直到100 MB是没有问题的,我的配置完美的作品,但高于100 MB,它抛出的System.OutOfMemoryException。

该上传方法适用于的FileStream,但在此之前,我将文件保存到临时文件夹。不知道这是一个问题,还是其他什么东西。我想补充我的控制器,这需要调用WCF服务的护理code。

  [HttpPost]
    公众的ActionResult上传()
    {
        如果(Request.Files.Count大于0)
        {
            var文件= Request.Files [0];            如果(文件= NULL&放大器;!&安培; file.ContentLength大于0)
            {
                字符串文件名= Path.GetFileName(file.FileName);
                VAR路径= Path.Combine(使用Server.Mappath(〜/ App_Data文件/图片),文件名);                file.SaveAs(路径);                的FileStream fsSource =新的FileStream(路径,FileMode.Open,FileAccess.Read,FileShare.Read);                TileService.TileServiceClient客户端=新TileService.TileServiceClient();
                client.Open();
                client.UploadFile(文件名,fsSource);
                client.Close();                fsSource.Dispose();
                如果(System.IO.File.Exists(路径))
                {
                    System.IO.File.Delete(路径);
                }
            }
        }        返回RedirectToAction();
    }

该方法被称为是这样的:

  @using(Html.BeginForm(上传,家,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
<输入类型=文件名称=FileUploader/>
< BR />
<输入类型=提交名称=提交ID =提交值=上传文件/>
}

在ASP.NET web.config中我已经设置了以下几件事: executionTimeout,maxRequestLength的,requestLengthDiskThreshold,maxAllowedContentLength 的。我添加配置的结合部分。

 <&basicHttpBinding的GT;
    <绑定名称=BasicHttpBinding_ITileService
      closeTimeout =24:01:00openTimeout =24:01:00receiveTimeout =24:10:00的SendTimeout =24:01:00allowCookies =假bypassProxyOnLocal =假hostNameComparisonMode =StrongWildcard maxBufferPoolSize =4294967295MAXBUFFERSIZE =2147483647maxReceivedMessageSize =4294967295textEncoding =UTF-8transferMode =流useDefaultWebProxy =真messageEncoding =TEXT>
      < readerQuotas MAXDEPTH =2147483647maxStringContentLength =2147483647maxArrayLength =2147483647maxBytesPerRead =2147483647maxNameTableCharCount =2147483647/>
      <安全模式=无>
        <运输clientCredentialType =无proxyCredentialType =无的境界=/>
        <消息clientCredentialType =用户名algorithmSuite =默认/>
      < /安全>
    < /&结合GT;
  < / basicHttpBinding的>


解决方案

这个问题是不是在code,我认为。 ASP.NET项目在IIS防爆preSS,而不是本地IIS托管。由于我改变了在项目属性一切工程进展顺利。

我现在使用@nimeshjm的code虽然。感谢您的帮助!

I'd like to upload big files via ASP.NET to WCF service. Until 100 MB is not a problem, my configuration works perfectly, but above 100 MB it throws System.OutOfMemoryException.

The uploading method works with FileStream, but before that, I save the file to a temporary folder. Not sure if this is the problem, or something else. I add the code of my controller, which takes care of calling the wcf service.

[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("");
    }

The method is called like this:

@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" />
}

In the ASP.NET web.config I already set the following things: executionTimeout, maxRequestLength, requestLengthDiskThreshold, maxAllowedContentLength. I add the binding part of the configuration.

<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>

解决方案

The problem was not in the code I think. The ASP.NET project was hosted in IIS Express instead of Local IIS. Since I changed that in project properties everything works smoothly.

I'm using now @nimeshjm 's code though. Thanks for your help!

这篇关于asp.net上传大文件:抛出的System.OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆