In MVC, I need to make restriction to upload file limit size should not exceed 5 MB.
Here, i need to validate and restrict at client side only if exceed 5MB size limit.
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 ?
To check when a file is uploaded, you could add in a web.config key:
<add key="maxRequestLength" value="5242880"/> <!-- in Bytes -->
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
}
In addition, you can enforce a site-wide limit in Web.Config via the following:
<system.web>
<httpRuntime maxRequestLength="5120" /> <!-- in Kb -->
...
The top variable just allows you to nicely manage your on screen error though.
Check java script/ JQuery validation:
$('#file_select')[0].files[0], file.size
check test code here : [Click Here][1]
[1]: http://jsfiddle.net/rq8nA/
$("input:file").change(function () {
if ($(this).val() !== "") {
var file = $('#file_select')[0].files[0];
if(file.size > 5000) {
$('#err').html("Could not upload more than 5kb");
}
}
});
<input type="file" name="file" id="file_select" />
<span id="err"></span>