上传文件大小超过2 MB时,网站崩溃(VS 2010,.Net 4.0)。 [英] Site crashes when uploading file size is more than 2 MB (VS 2010, .Net 4.0).

查看:130
本文介绍了上传文件大小超过2 MB时,网站崩溃(VS 2010,.Net 4.0)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网站的文件上传大小限制为2 MB。为此,我尝试了两种方法,两种方法都失败了。



在第一种方法中,我将web.config文件中的httpRuntime元素的maxRequestLength属性设置为2048 KB。在相应页面上传期间,我检查上传文件的大小,并允许上传,如果它在允许的限制范围内。



例如:



I’m trying to limit the file upload size of a site to 2 MB. For this, I tried two methods, both of which failed.

In the first method, I set the maxRequestLength attribute of the httpRuntime element in the web.config file to 2048 KB. During the upload in the corresponding page, I check the size of the uploading file and allow upload if it’s within the permissible limit.

Example:

<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <pages styleSheetTheme="Default"></pages>
    <httpRuntime maxRequestLength = "2048" />
  </system.web>
  <connectionStrings>
    <add name="TestConnection" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;password=" />
  </connectionStrings>
</configuration>





在页面的代码编辑器中:





In the code editor of the page:

'Check if the size of the file is within 2 MB (2097152 in bytes).
If ImageUpload.PostedFile.ContentLength <= 2097152 Then
	'Code to upload…
End If





如果文件大小在2 MB以内,那么一切都很顺利。一旦文件大小超过限制,它就会崩溃。作为一种解决方法,我将web.config中的maxRequestLength增加到更高的值--35840 KB(35 MB),同时保持页面中的验证方法不变。这次,实际内存为35 MB或更小的任何文件都通过页面方法以2 MB的约束正确验证。但是,一旦文件的实际内存超过35 MB,网站就会再次崩溃。



在第二种方法中,我向网站添加了一个Global.asax文件。在其Application_Error方法中,我检查是否由于超过最大请求长度而引发异常。如果是,那么它应该重定向到引发异常的同一页面,并在查询字符串中显示错误消息。当页面重新加载时,它应显示错误消息。

但是在运行时,虽然Application_Error方法正在执行,但它再次崩溃。



Global.asax示例:





If the file size is within 2 MB then everything goes fine. As soon as the file size exceeds the limit, it crashes. As a workaround, I increased maxRequestLength in web.config to a much higher value- 35840 KB (35 MB) while keeping the validation method in the page intact. This time, any file with actual memory of 35 MB or less is validated properly with a constraint of 2 MB by the page method. But as soon the actual memory of the file exceeds 35 MB the site crashes again.

In the second method, I added a Global.asax file to the site. In its Application_Error method I check if the exception is raised due to exceeding of the maximum request length. If yes, then it should redirect to the same page from which the exception is raised with an error message in the query string. When the page re-loads it should show the error message.
But during runtime, though the Application_Error method is getting executed, it crashes again.

Global.asax example:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs.

        ' Handle errors that occur while uploading files larger than permissible 2 MB limit.
        If HttpContext.Current.Request.Url.ToString.Contains("Customer.aspx") Then
            If HttpContext.Current.Error.InnerException.Message.Contains("Maximum request length exceeded") Then
                HttpContext.Current.ClearError()
                HttpContext.Current.Response.Redirect(String.Format("~/Customer.aspx?ErrorMessage={0}", "The file cannot be uploaded because it exceeds the 2 MB size limit."))
            End If
        End If
    End Sub





请帮助解决这个问题。问候。



Please help to sort out this problem. Regards.

推荐答案

你得到的错误是什么?应用程序池崩溃了吗?服务器崩溃了吗?是否有错误消息?



您是否看过executionTimeout,因为它可能是超时问题而不是尺寸问题?



What is the error you are getting? Is the App pool crashing? Is the server crashing? Is there an error message?

Have you looked at executionTimeout as it maybe a timeout issue not size issue?

<configuration>
  <system. web=""> 
    <httpruntime maxrequestlength="2048" executiontimeout="3600" />
  br mode="hold" /></system.></configuration>





你是否也看过设置服务器maxAllowedContentLength(IIS7)?





Have you also looked at setting the server maxAllowedContentLength (IIS7) ?

<system.webserver>
  <security>
    <requestfiltering>
       <requestlimits maxallowedcontentlength="Big number here" />
    </requestfiltering>
 </security>
</system.webserver>





这些链接怎么样?



http://blogs.msdn.com/b/prashant_upadhyay/archive/201 1/07/13 / large-file-upload-issue-in-asp-net.aspx [ ^ ]



http://www.webdavsystem.com/server/documentation/large_files_iis_asp_net [ ^ ]


我尝试使用executionTimeout, requestLengthDiskThreshold和maxAllowedContentLength。



I tried using executionTimeout, requestLengthDiskThreshold and maxAllowedContentLength.

<configuration>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <pages styleSheetTheme="Default"></pages>
    <httpRuntime maxRequestLength = "2048" executionTimeout="3600" requestLengthDiskThreshold="2048" />
  </system.web>
  <connectionStrings>
    <add name="TestConnection" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=sa;password=" />
  </connectionStrings>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="20480000"></requestLimits>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>





但是同样的错误发生了。请在以下链接中找到错误的截图:



Chrome 26.0.1410.43

https://docs.google.com/file/d/0B3aGOxBwxg17RDN3blQ5T1FTOWc/edit?usp=sharing [ ^ ]



Firefox 14.0.1

https: //docs.google.com/file/d/0B3aGOxBwxg17VlIyUXptaUliQ3c/edit?usp=sharing [ ^ ]



Internet Explorer 8

https://docs.google.com/file/d/0B3aGOxBwxg17Ri1CYVJHS3IxUDg/edit?usp=sharing [ ^ ]



Safari 5.1.7

https://docs.google.com/file/d/0B3aGOxBwxg17U1UzZFZOc3d3SFU/edit?usp=sharing [ ^ ]



来自@cigwork和 http://blogs.msdn.com /b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx ,似乎不能按我想要的方式完成。我们必须寻求解决方法。我能想到的是,将maxRequestLength设置为一个很大的值,例如:在web.config中104857600(100 GB)并分别在相应页面中验证上传文件大小为2 MB。



如果有人想出更好的主意,请分享。

谢谢!



But the same error occurs. Please find the screenshots of the error in different browsers in the links below:

Chrome 26.0.1410.43
https://docs.google.com/file/d/0B3aGOxBwxg17RDN3blQ5T1FTOWc/edit?usp=sharing[^]

Firefox 14.0.1
https://docs.google.com/file/d/0B3aGOxBwxg17VlIyUXptaUliQ3c/edit?usp=sharing[^]

Internet Explorer 8
https://docs.google.com/file/d/0B3aGOxBwxg17Ri1CYVJHS3IxUDg/edit?usp=sharing[^]

Safari 5.1.7
https://docs.google.com/file/d/0B3aGOxBwxg17U1UzZFZOc3d3SFU/edit?usp=sharing[^]

From the comment of @cigwork and http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx, it seems it cannot be done the way I want. We’ll have to go for a workaround. The one that I can think of is, set maxRequestLength to a big value e.g. 104857600 (100 GB) in web.config and validate uploading file size for 2 MB in the respective page separately.

Please share if anybody comes up with a better idea.
Thanks!


这篇关于上传文件大小超过2 MB时,网站崩溃(VS 2010,.Net 4.0)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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