在asp.net中应用服务器端的文件上传大小限制C#
问题描述:
我正在应用文件上传大小限制,该用户无法上传超过30 MB的文件,并且如果超过了限制,则向他发送消息。 我正在使用以下代码。在asp.net中应用服务器端的文件上传大小限制C#
if (fileUpload.HasFile)
{
if (fileUpload.PostedFile.ContentLength < 30 * 1024 * 1024) // 30 MB
{
if (fileUpload.FileName != null && fileUpload.FileName != "")
{
UploadFile(fileUpload, "flv,mp3", out videoFileName, out uploadError);
if (uploadError != "")
{
lblMessage.Visible = true;
lblMessage.Text = uploadError;
return false;
}
}
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "File size exceeds the Limits. Please try uploading smaller size file.";
return false;
}
}
此代码工作在Visual Studio很好,但是当我部署在IIS上的应用程序,它并没有给我任何消息,如果我给超过30 MB的文件,并直接上传文件。
我做错了。
问候, 卡辛
答
你使用了标准的maxRequestLength配置属性吗?很可能它不适合您的需求,但它会在不利用资源达到文件限制的情况下更好地工作。
有关如何处理explanantion错误看到A better way of handling maxRequestLength exceptions
<system.web>
<httpRuntime maxRequestLength="31457280" executionTimeout="360"/>
</system.web>
答
在IIS7下运行,您可以在web.config文件中设置文件上传大小限制这样
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="10485760"/>
</requestFiltering>
</security> </system.webServer>
但是这不限制用户。 – Kash 2012-03-08 10:20:10
不,内置于浏览器的标准文件上传控件不允许文件大小限制。你需要第三方(flash/applet/silverlight等)来做到这一点。 – 2012-03-08 12:18:42