首页 文章

如何在ASP.NET中增加最大上传文件大小?

提问于
浏览
203

我有一个表单,除了ASP.NET中的文件上传 . 我需要将最大上传大小增加到4 MB以上 .

我发现在某些地方引用了以下代码msdn .

[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]

没有一个参考文献真正描述了如何使用它,我尝试了几件事但没有成功 . 我只想为要求文件上传的某些页面修改此属性 .

这是正确的路线吗?我该如何使用它?

14 回答

  • 2

    此设置位于web.config文件中 . 它影响整个应用程序,但是......我不认为你可以在每页设置它 .

    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="xxx" />
      </system.web>
    </configuration>
    

    “xxx”以KB为单位 . 默认值为4096(= 4 MB) .

  • 7

    对于IIS 7,以及添加httpRuntime maxRequestLength设置,您还需要添加:

    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="52428800" /> <!--50MB-->
          </requestFiltering>
        </security>
      </system.webServer>
    

    或者在IIS(7)中:

    选择要启用以接受大文件上载的网站 . 在主窗口中双击“请求过滤”选择“编辑功能设置”修改“允许的最大内容长度(字节)”

  • 0

    为了增加上传文件的大小限制,我们有两种方法

    1. IIS6 or lower

    默认情况下,在ASP.Net中,要上传到服务器的文件的最大大小约为4MB . 可以通过修改web.config中的maxRequestLength属性来增加此值 . 请记住:maxRequestLenght以KB为单位

    Example :如果要将上传限制为15MB,请将maxRequestLength设置为“15360”(15 x 1024) .

    <system.web>
       <!-- maxRequestLength for asp.net, in KB --> 
       <httpRuntime maxRequestLength="15360" ></httpRuntime> 
    </system.web>
    

    2. IIS7 or higher

    这里用来上传文件的方式略有不同.IIS7引入了请求过滤模块 . 在ASP.Net.Means之前执行管道工作的方式是先检查IIS值(maxAllowedContentLength)然后检查ASP.NET值(maxRequestLength) .maxAllowedContentLength属性默认为28.61 MB . 可以通过修改同一web.config中的两个属性来增加此值 . 请记住:maxAllowedContentLength以字节为单位

    Example :如果要将上传限制为15MB,请将maxRequestLength设置为“15360”,将maxAllowedContentLength设置为"15728640"(15 x 1024 x 1024) .

    <system.web>
       <!-- maxRequestLength for asp.net, in KB --> 
       <httpRuntime maxRequestLength="15360" ></httpRuntime> 
    </system.web>
    
    <system.webServer>              
       <security> 
          <requestFiltering> 
             <!-- maxAllowedContentLength, for IIS, in bytes --> 
             <requestLimits maxAllowedContentLength="15728640" ></requestLimits>
          </requestFiltering> 
       </security>
    </system.webServer>
    

    MSDN Reference linkhttps://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

  • 0

    我相信web.config中的这一行将设置最大上传大小:

    <system.web>
    
            <httpRuntime maxRequestLength="600000"/>
    </system.web>
    
  • 2

    对于2Gb最大限制,在您的应用程序web.config上:

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
    </system.web>
    
    <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="2147483647" />
        </requestFiltering>
      </security>
    </system.webServer>
    
  • 3

    如果它的Windows 2003 / IIS 6.0然后检查位于文件夹C:\ windows \ system32 \ inetsrv \的文件 metabase.xml 中的AspMaxRequestEntityAllowed = "204800"

    对于大多数用户来说,默认值“204800”(~205Kb)对我来说太低了 . 只需将值更改为您认为应该最大的值 .

    如果在编辑后无法保存文件,则必须停止ISS服务器或启用服务器以允许编辑文件:

    alt text http://blogs.itmaskinen.se/image.axd?picture=WindowsLiveWriter/Request.BinaryReadFailedwindows2003IIS.0_BC5A/image_2.png

    编辑:我没有正确阅读问题(如何在webconfig中设置maxrequest) . 但是这个信息可能是其他人的骚扰,很多人将他们的网站从win2000-server移动到win2003并且有一个有效的上传功能,突然得到 Request.BinaryRead Failed 错误将使用它 . 所以我在这里留下答案 .

  • 0

    我在win 2008 IIS服务器中遇到同样的问题,我已经解决了在web.config中添加此配置的问题:

    <system.web>
        <httpRuntime executionTimeout="3600" maxRequestLength="102400" 
         appRequestQueueLimit="100" requestValidationMode="2.0"
         requestLengthDiskThreshold="10024000"/>
    </system.web>
    

    requestLengthDiskThreshold 默认为80000字节,因此对于我的应用程序来说太小了 . requestLengthDiskThreshold以字节为单位,maxRequestLength以千字节为单位 .

    如果应用程序使用 System.Web.UI.HtmlControls.HtmlInputFile 服务器组件,则会出现此问题 . 增加requestLengthDiskThreshold是解决它的必要条件 .

  • 151

    如果您使用的是Framework 4.6

    <httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" maxRequestLength="10485760"  />
    
  • 354

    您可以在应用程序web.config文件中编写该代码块 .

    <httpRuntime maxRequestLength="2048576000" />
    <sessionState timeout="3600"  />
    

    通过编写该代码,您可以上传比现在更大的文件

  • 4

    我知道这是一个老问题 .

    所以这就是你要做的:

    在您的web.config文件中,将其添加到:

    <!-- 3GB Files / in kilobyte (3072*1024) -->
        <httpRuntime targetFramework="4.5" maxRequestLength="3145728"/>
    

    这下

    <security>
        <requestFiltering>
    
          <!-- 3GB Files / in byte (3072*1024*1024) -->
          <requestLimits maxAllowedContentLength="3221225472" />
    
        </requestFiltering>
    </security>
    

    你在评论中看到它是如何工作的 . 在一个中你需要以字节为单位,而另一个以千字节为单位 . 希望有所帮助 .

  • 0

    如果您使用sharepoint,则还应使用管理工具配置最大大小:kb925083

  • 57

    我有一篇关于如何increase the file size for asp upload control的博客文章 .

    从帖子:

    默认情况下,FileUpload控件允许上载最大4MB的文件,执行超时为110秒 . 可以在web.config文件的httpRuntime部分中更改这些属性 . maxRequestLength属性确定可以上载的最大文件大小 . 该executionTimeout属性确定执行的最长时间 .

  • 14

    如果它在您的本地计算机上工作,并且在IIS中部署后不起作用(我使用的是Windows Server 2008 R2),我有一个解决方案 .

    打开IIS(inetmgr)转到您的网站在右侧转到内容(请求过滤)转到编辑功能设置将最大内容大小更改为(您需要的字节数)这将起作用 . 您也可以从以下主题获取帮助http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

  • 9

    最大文件大小可以限制为单个MVC控制器,甚至可以限制为Action .
    web.config <location>标签可用于此:

    <location path="YourAreaName/YourControllerName>/YourActionName>">
      <system.web>
        <!-- 15MB maxRequestLength for asp.net, in KB 15360 -->
        <httpRuntime maxRequestLength="15360" />
      </system.web>
      <system.webServer>
        <security>
          <requestFiltering>
            <!-- 15MB maxAllowedContentLength, for IIS, in bytes 15728640 -->
            <requestLimits maxAllowedContentLength="15728640" />
          </requestFiltering>
        </security>
      </system.webServer>
    </location>
    

    或者您可以在区域自己的web.config中添加这些条目 .

相关问题