简体   繁体   English

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

[英]How to increase the max upload file size in ASP.NET?

I have a form that excepts a file upload in ASP.NET.我有一个表单,除了 ASP.NET 中的文件上传。 I need to increase the max upload size to above the 4 MB default.我需要将最大上传大小增加到默认值 4 MB 以上。

I have found in certain places referencing the below code at msdn .我在某些地方发现在msdn 中引用了以下代码。

[ConfigurationPropertyAttribute("maxRequestLength", DefaultValue = )]

None of the references actually describe how to use it, and I have tried several things with no success.没有一个参考文献真正描述了如何使用它,我尝试了几件事但没有成功。 I only want to modify this attribute for certain pages that are asking for file upload.我只想为某些要求文件上传的页面修改此属性。

Is this the correct route to take?这是正确的路线吗? And how do I use this?我该如何使用它?

This setting goes in your web.config file.此设置位于您的 web.config 文件中。 It affects the entire application, though... I don't think you can set it per page.但是,它会影响整个应用程序……我认为您不能按页面设置它。

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

"xxx" is in KB. “xxx”以 KB 为单位。 The default is 4096 (= 4 MB).默认值为 4096 (= 4 MB)。

For IIS 7+, as well as adding the httpRuntime maxRequestLength setting you also need to add:对于 IIS 7+,以及添加 httpRuntime maxRequestLength 设置,您还需要添加:

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

Or in IIS (7):或在 IIS (7) 中:

  • Select the website you want enable to accept large file uploads.选择您要启用以接受大文件上传的网站。
  • In the main window double click 'Request filtering'在主窗口中双击“请求过滤”
  • Select "Edit Feature Settings"选择“编辑功能设置”
  • Modify the "Maximum allowed content length (bytes)"修改“最大允许内容长度(字节)”

To increase uploading file's size limit we have two ways要增加上传文件的大小限制,我们有两种方法

1. IIS6 or lower 1. IIS6 或更低

By default, in ASP.Net the maximum size of a file to be uploaded to the server is around 4MB .默认情况下,在 ASP.Net 中,要上传到服务器的文件的最大大小约为4MB This value can be increased by modifying the maxRequestLength attribute in web.config .可以通过修改web.config 中maxRequestLength属性来增加此值。

Remember : maxRequestLenght is in KB请记住:maxRequestLenght 以 KB 为单位

Example : if you want to restrict uploads to 15MB, set maxRequestLength to “15360” (15 x 1024).示例:如果您想将上传限制为 15MB,请将 maxRequestLength 设置为“15360”(15 x 1024)。

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

2. IIS7 or higher 2. IIS7 或更高版本

A slight different way used here to upload files.IIS7 has introduced request filtering module .Which executed before ASP.Net.Means the way pipeline works is that the IIS value( maxAllowedContentLength ) checked first then ASP.NET value( maxRequestLength ) is checked.The maxAllowedContentLength attribute defaults to 28.61 MB .This value can be increased by modifying both attribute in same web.config .这里使用的上传文件的方式略有不同。IIS7 引入了请求过滤模块。它在 ASP.Net 之前执行。意味着管道的工作方式是首先检查 IIS 值( maxAllowedContentLength ),然后检查 ASP.NET 值( maxRequestLength )。 maxAllowedContentLength 属性默认为28.61 MB 。可以通过修改同一web.config 中的两个属性来增加此值。

Remember : maxAllowedContentLength is in bytes请记住:maxAllowedContentLength 以字节为单位

Example : if you want to restrict uploads to 15MB, set maxRequestLength to “15360” and maxAllowedContentLength to "15728640" (15 x 1024 x 1024).示例:如果您想将上传限制为 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 link : https://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx MSDN 参考链接https : //msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

I believe this line in the Web.config will set the max upload size:我相信Web.config中的这一行将设置最大上传大小:

<system.web>

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

for a 2 GB max limit, on your application web.config:对于 2 GB 的最大限制,在您的应用程序 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>

If its windows 2003 / IIS 6.0 then check out AspMaxRequestEntityAllowed = "204800" in the file metabase.xml located in folder C:\\windows\\system32\\inetsrv\\如果它的 Windows 2003 / IIS 6.0 则检查位于文件夹 C:\\windows\\system32\\inetsrv\\ 中的文件metabase.xml中的 AspMaxRequestEntityAllowed = "204800"

The default value of "204800" (~205Kb) is in my opinion too low for most users. “204800”(~205Kb)的默认值在我看来对于大多数用户来说太低了。 Just change the value to what you think should be max.只需将值更改为您认为应该最大的值。

If you cant save the file after editing it you have to either stop the ISS-server or enable the server to allow editing of the file:如果编辑后无法保存文件,则必须停止 ISS 服务器或启用服务器以允许编辑文件:

替代文字
(source: itmaskinen.se ) (来源: itmaskinen.se

Edit: I did not read the question correct (how to set the maxrequest in webconfig).编辑:我没有正确阅读问题(如何在 webconfig 中设置 maxrequest)。 But this informatin may be of interrest for other people, many people who move their sites from win2000-server to win2003 and had a working upload-function and suddenly got the Request.BinaryRead Failed error will have use of it.但是这个信息可能对其他人很感兴趣,许多将他们的站点从 win2000-server 移动到 win2003 并具有工作上传功能并突然收到Request.BinaryRead Failed错误的人都会使用它。 So I leave the answer here.所以我把答案留在这里。

I've the same problem in a win 2008 IIS server, I've solved the problem adding this configuration in the web.config:我在 win 2008 IIS 服务器中遇到了同样的问题,我已经解决了在 web.config 中添加此配置的问题:

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

The requestLengthDiskThreshold by default is 80000 bytes so it's too small for my application. requestLengthDiskThreshold默认为 80000 字节,因此对于我的应用程序来说太小了。 requestLengthDiskThreshold is measured in bytes and maxRequestLength is expressed in Kbytes. requestLengthDiskThreshold 以字节为单位,maxRequestLength 以千字节表示。

The problem is present if the application is using a System.Web.UI.HtmlControls.HtmlInputFile server component.如果应用程序使用System.Web.UI.HtmlControls.HtmlInputFile服务器组件,则会出现此问题。 Increasing the requestLengthDiskThreshold is necessary to solve it.需要增加 requestLengthDiskThreshold 来解决它。

Max file size can be restricted to a single MVC Controller or even to an Action.最大文件大小可以限制为单个 MVC 控制器甚至是一个操作。
web.config <location> tag can be used for this: 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>

Or you can add these entries in area's own web.config.或者您可以在区域自己的 web.config 中添加这些条目。

I know it is an old question.我知道这是一个老问题。

So this is what you have to do:所以这是你必须做的:

In you web.config file, add this in <system.web> :在您的 web.config 文件中,将其添加到<system.web>

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

and this under <system.webServer> :这在<system.webServer>

<security>
    <requestFiltering>

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

    </requestFiltering>
</security>

You see in the comment how this works.你在评论中看到这是如何工作的。 In one you need to have the sie in bytes and in the other one in kilobytes.在一个中,您需要以字节为单位,而在另一个中以千字节为单位。 Hope that helps.希望有帮助。

如果您使用的是 Framework 4.6

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

You can write that block of code in your application web.config file.您可以在应用程序 web.config 文件中编写该代码块。

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

By writing that code you can upload a larger file than now通过编写该代码,您可以上传比现在更大的文件

I have a blog post on how to increase the file size for asp upload control .我有一篇关于如何增加 asp 上传控制的文件大小的博客文章。

From the post:从帖子:

By default, the FileUpload control allows a maximum of 4MB file to be uploaded and the execution timeout is 110 seconds.默认情况下,FileUpload 控件允许上传最大 4MB 的文件,执行超时为 110 秒。 These properties can be changed from within the web.config file's httpRuntime section.可以从 web.config 文件的 httpRuntime 部分更改这些属性。 The maxRequestLength property determines the maximum file size that can be uploaded. maxRequestLength 属性确定可以上传的最大文件大小。 The executionTimeout property determines the maximum time for execution. executionTimeout 属性确定执行的最长时间。

If it works in your local machine and does not work after deployment in IIS (i used Windows Server 2008 R2) i have a solution.如果它在您的本地机器上工作并且在 IIS 中部署后不起作用(我使用的是 Windows Server 2008 R2),我有一个解决方案。

Open IIS (inetmgr) Go to your website At right hand side go to Content (Request Filtering) Go to Edit Feature Settings Change maximum content size as (Bytes you required) This will work.打开 IIS (inetmgr) 转到您的网站 在右侧转到内容(请求过滤) 转到编辑功能设置 将最大内容大小更改为(所需字节数) 这将起作用。 You can also take help from following thread http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits您还可以从以下线程获得帮助http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

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

如果您使用 ssl cert 您的 ssl 管道 F5 设置配置。重要。

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

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