asp.net如何捕获上传文件过大的错误
如题,asp.net如何捕获上传文件过大的错误,如果文件大于设置的大小了什么都没有,使用者 也不知道是什么错误。网上说的用JS的办法, 但是附件不只 图片还有其他类型的文件,并且JS获取文件大小还有可能不兼容浏览器和 浏览器安全性设置的问题。
求解
--------------------编程问答-------------------- 啥意思??判断上传文件大小?
给你个 上传共通方法 自己可改改 我这里是上传图片
--------------------编程问答--------------------
/// <summary>
/// 上传图片
/// </summary>
/// <param name="fileUp">上传控件</param>
/// <param name="fileExtension">上传文件类型</param>
public static int UpFile(System.Web.UI.Page page, System.Web.UI.WebControls.FileUpload fileUp, string fileExtension, string strNewName)
{
//上传结果 0未知 1成功 -1文件格式有误 -2文件太大
int strValue = 0;
string fileName = fileUp.FileName;
FileInfo fileinfo = new FileInfo(fileName);
switch (fileExtension)
{
case "img":
if (fileinfo.Extension.ToLower() == ".gif" || fileinfo.Extension.ToLower() == ".jpg")
{
if (fileUp.PostedFile.ContentLength <= Convert.ToInt64(Common.GetMes.GetConfigAppValue("ImageSize")))
{
UpLoadFile(page, fileUp, Common.GetMes.GetConfigAppValue("AdImgURL"), strNewName);
strValue = 1;
}
else
{
return -2; //文件太大 !
}
}
else
{
return -1; //格式有误!
}
break;
default:
break;
}
return strValue;
}
可以这样做
图片和文件一样的改下就行了 --------------------编程问答-------------------- 如果选择的文件体积大于web.config文件中设置的上传文件大小,提交时引发类型为“System.Web.HttpUnhandledException”的异常。可以在Global.asax中写
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
if (Server.GetLastError().Message.Contains("HttpUnhandledException"))
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "abc.txt", "上传文件过大!");
}
这里写入网站根目录一个文件,你可以改成你要做的事情。 --------------------编程问答-------------------- ASP.NET 用.Net的File控件上传文件大文件上传之异常处理 的解决方案
private void Application_BeginRequest(Object source, EventArgs e)
2 {
3 HttpRequest request = HttpContext.Current.Request;
4 if (request.ContentLength > 4096000)//4096000 is maxRequestLength
5 {
6 HttpApplication app = source as HttpApplication;
7 HttpContext context = app.Context;
8
9 HttpWorkerRequest wr =
10 (HttpWorkerRequest)(context.GetType().GetProperty
11 ("WorkerRequest", BindingFlags.Instance |
12 BindingFlags.NonPublic).GetValue(context, null));
13 byte[] buffer;
14 if (wr.HasEntityBody())
15 {
16 int contentlen = Convert.ToInt32(wr.GetKnownRequestHeader(
17
18 HttpWorkerRequest.HeaderContentLength));
19 buffer = wr.GetPreloadedEntityBody();
20 int received = buffer.Length;
21 int totalrecv = received;
22 if (!wr.IsEntireEntityBodyIsPreloaded())
23 {
24 buffer = new byte[65535];
25 while ( contentlen - totalrecv >= received )
26 {
27 received =
28 wr.ReadEntityBody(buffer,
29 buffer.Length);
30 totalrecv += received;
31 }
32 received =
33 wr.ReadEntityBody(buffer, contentlen - totalrecv);
34 }
35 }
36 context.Response.Redirect("../error.aspx");//Redirect to custom error page.
37 }
http://blog.csdn.net/binyao02123202/article/details/5956442 --------------------编程问答-------------------- <configuration>
<system.web>
<processModel memoryLimit="80"/>
</system.web>
</configuration>
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>
</configuration>
补充:.NET技术 , ASP.NET