使用 asp:FileUpLoad 控件上传文件时文件太大

File too big when uploading a file with the asp:FileUpLoad control(使用 asp:FileUpLoad 控件上传文件时文件太大)

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

问题描述

我正在使用 asp:FileUpLoad 在我的 asp.net c# 项目中上传文件.只要文件大小不超过允许的最大值,这一切都可以正常工作.超过最大值时.我收到错误Internet Explorer 无法显示网页".问题是 try catch 块没有捕获错误,所以我不能给 user 一个友好的消息,他们已经超出了允许的大小.我在网上搜索时看到了这个问题,但我找不到可接受的解决方案.

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.

鉴于建议ajac的答案,我需要添加此评论.几个月前我尝试加载 ajax 控件.一旦我使用 ajax 控件,我就会收到这个编译错误.

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.

错误 98 类型System.Web.UI.ScriptControl"是在未引用的程序集.您必须添加对程序集的引用'System.Web.Extensions,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35'.

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

虽然我确实添加了System.Web.Extensions",但我可以摆脱它.所以我放弃了 Ajax 并使用了其他技术.

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.

推荐答案

默认文件大小限制为 (4MB),但您可以通过在上传页面所在的目录中放置 web.config 文件以本地化方式更改默认限制.这样您就不必让整个网站允许大量上传(这样做会使您容易受到某些类型的攻击).

The default file size limit is (4MB) but you can change the default limit in a localized way by dropping a web.config file 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).

只需在 部分下的 web.config 中设置即可.例如在下面的示例中,我将最大长度设置为 2GB

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

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

请注意,maxRequestLength 以 KB 为单位设置,并且可以设置为 2GB (2079152 KB).实际上我们并不经常需要设置2GB的请求长度,但是如果你设置的请求长度更高,我们也需要增加executionTimeout.

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 the 2GB request length, but if you set the request length higher, we also need to increase the executionTimeout.

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

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

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

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

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

你可以这样做...

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 控件上传文件时文件太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 asp:FileUpLoad 控件上传文件时文件太大

基础教程推荐