上传与ASP文件时文件过大:FileUpload控件 [英] File too big when uploading a file with the asp:FileUpLoad control

查看:410
本文介绍了上传与ASP文件时文件过大:FileUpload控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是 ASP:文件上传来上传文件在我的 asp.net C#的项目。这一切工作正常,只要文件大小不超过允许的最大值。当超过最大值。我得到一个错误 Internet Explorer无法显示该网页。问题是try catch块未捕获错误,所以我不能给用户友好的消息,他们已经excced允许的大小。我已经看到了这个问题,同时在网上搜索,但我无法找到一个可以接受的解决办法。

我会看看其他的控制,但我managemment可能不会去购买第三方控制。

在光的回答暗示AJAC,我需要添加此评论。我试图加载的AJAX控件个月前。当我使用AJAX的控制,我得到这个编译错误。

  

98错误类型System.Web.UI.ScriptControl'是一个定义   未引用的程序集。您必须添加一个引用组件   System.Web.Extensions程序,版本= 4.0.0.0,文化=中立,   公钥= 31bf3856ad364e35'。

我能摆脱它,虽然我没有加 System.Web.Extensions程序。于是,我放弃了Ajax和使用其他技术。

所以我需要解决这个问题,或者一个全新的解决方案。

解决方案
  

默认文件大小限制为(4MB),但你可以在一个本地化的方式通过在目录中,你的上传页面的生活投下的web.config更改默认的限制。这样,你不必让你的整个网站允许大量上传(这样做会打开你一定类型的攻击)。

只需设置在web.config中下<的System.Web> 部分。例如在下面的例子中我设置的最大长度为 2GB

 <的httpRuntime maxRequestLength的=2097152executionTimeout =600/>
 

请注意,的maxRequestLength 设置以KB为单位的,它可以被设置为 2GB(2079152 KB的) 。实际上我们并不经常需要设定 2GB 请求长度,但如果你设置的要求高出一截,我们还需要增加 executionTimeout

执行超时指定的请求被允许ASP.NET被自动关闭之前执行的最大秒数。 (默认时间为110秒。)

有关详细信息,请阅读的httpRuntime元素(ASP.NET设置架构)

现在,如果你想显示自定义消息用户,如果文件大小大于 100MB

您可以这样做。

 如果(FileUpload1.HasFile&安培;&安培; FileUpload1.PostedFile.ContentLength> 104857600)
{
    //FileUpload1.PostedFile.ContentLength  - 返回字节大小
    lblMsg.Text =你只能上传文件最大为100 MB。
}
 

I am using the asp:FileUpLoad to upload files in my asp.net c# project. This all works fine as long as the file size does not exceed the maximum allowed. When the maximum is exceeded. I get an error "Internet Explorer cannot display the webpage". The problem is the try catch block doesn't catch the error so I cannot give the user a friendly message that they have excced the allowable size. I have seen this problem while searching the web but I cannot find an acceptable solution.

I would look at other controls, but my managemment probably wouldn't go for buying a third-party control.

In light of answer suggesting ajac, I need to add this comment. I tried to load the ajax controls months ago. As soon as I use an ajax control, I get this compile error.

Error 98 The type 'System.Web.UI.ScriptControl' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

I could get rid of it although I did add 'System.Web.Extensions'. So I abandoned Ajax and used other techniques.

So I need to solved this problem or a completely new solution.

解决方案

Default file size limit is (4MB) but you can change the default limit in a localized way by dropping a web.config in the directory where your upload page lives. That way you don't have to make your whole site allow huge uploads (doing so would open you up to a certain kinds of attacks).

Just set in web.config under <system.web> section. e.g. In the below example I am setting the maximum length that is 2GB

<httpRuntime maxRequestLength="2097152" executionTimeout="600" />

Please note that the maxRequestLength is set in KB's and it can be set up to 2GB (2079152 KB's). Practically we don't often need to set 2GB request length, but if you set the request length higher, we also need to increase the executionTimeout.

Execution Timeout Specifies the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET. (Default time is 110 seconds.)

For Details please read httpRuntime Element (ASP.NET Settings Schema)

Now if you want to show the custom message to user, if the file size is greater than 100MB.

You can do like..

if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 104857600)
{
    //FileUpload1.PostedFile.ContentLength -- Return the size in bytes
    lblMsg.Text = "You can only upload file up to 100 MB.";
}

这篇关于上传与ASP文件时文件过大:FileUpload控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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