如果超过特定大小限制,如何防止文件上传? [英] How to prevent files to upload if exceed specific size limit?
问题描述
在MVC中,我需要限制上传文件的大小限制,不得超过5 MB。
In MVC, I need to make restriction to upload file limit size should not exceed 5 MB.
在这里,只有在以下情况下,我才需要在客户端进行验证和限制超过了5MB的大小限制。
Here, i need to validate and restrict at client side only if exceed 5MB size limit.
我可以使用ajax文件上传器来实现,但是它支持IE 10及更高版本,但是我需要为IE 9及更高版本提供支持。
I able to achieve using ajax file uploader but, it support IE 10 and above but, I delicately need to provide support for IE 9 and above.
请指导我如何在客户端或任何替代解决方案中进行验证?
Please guide how i can make the validation at client side or any alternative solution ?
推荐答案
要检查何时上传文件,可以添加web.config密钥:
To check when a file is uploaded, you could add in a web.config key:
<add key="maxRequestLength" value="5242880"/> <!-- in Bytes -->
然后在代码中发布文件时,其中 file
是 HttpPostedFileBase
:
then when you post a file in your code where file
is the HttpPostedFileBase
:
if (file.ContentLength > 0 && file.ContentLength < Convert.ToDouble(WebConfigurationManager.AppSettings["maxRequestLength"]) {
//do stuff
}
else {
//return error to view
}
此外,您可以在通过以下方式进行Web.Config:
In addition, you can enforce a site-wide limit in Web.Config via the following:
<system.web>
<httpRuntime maxRequestLength="5120" /> <!-- in Kb -->
...
top变量仅允许您很好地管理屏幕错误。
The top variable just allows you to nicely manage your on screen error though.
这篇关于如果超过特定大小限制,如何防止文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!