dsi
dsi

Reputation: 3359

How to prevent files to upload if exceed specific size limit?

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 ?

Upvotes: 4

Views: 6731

Answers (4)

m István
m István

Reputation: 23

You can check the ContentLength in the controller POST action

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Publication publication)
    {

        if (ModelState.IsValid)
        {
            if (publication.ImageUpload.ContentLength > (9))
            {
                ModelState.AddModelError("ImageData", "File size must be less than xxx MB");
                return View();
            }
            else
            { }

Upvotes: 0

user3240560
user3240560

Reputation: 360

Try this below code,

$(function () {

            $("[id*='Attachments_']").change(function () {

                if (undefined != this.files) {
                    var f = this.files[0]
                    var id = this.id;
                    var extArray = new Array("dll", "exe");
                    var ext = f.name.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1");
                    if (ext == extArray[0] || ext == extArray[1]) {                            
                        alert("System will not allow to upload the files with extenstion(.exe,.dll).Try to upload a different file.");
                        //reset file upload control                            
                        $("#" + this.id).replaceWith($("#" + this.id).clone(true));
                        return false;
                    } 
                    //here I CHECK if the FILE SIZE is bigger than 5 MB (numbers below are in bytes)
                    else if (f.size > 5242880 || f.fileSize > 5242880) {
                        //show an alert to the user
                        alert("Allowed file size exceeded. (Max. 5 MB)");

                        //reset file upload control                            
                        $("#" + this.id).replaceWith($("#" + this.id).clone(true));
                        return false;
                    }
                }
            })
        });

Upvotes: 0

Nayan
Nayan

Reputation: 126

    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>

Upvotes: 2

scgough
scgough

Reputation: 5252

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.

Upvotes: 3

Related Questions