0

I have a JQuery multi-file upload solution that requires restriction on file format and size.

Here's the JSFIDDLE, and the JS code follows below:

$(document).ready(function() {
  if (window.File && window.FileList && 
window.FileReader) {
    $("#files").on("change", function(e) {
      var files = e.target.files,
        filesLength = files.length;
      for (var i = 0; i < filesLength; i++) {
        var f = files[i]
        var fileReader = new FileReader();
        fileReader.onload = (function(e) {
          var file = e.target;
          $("<span class=\"pip\">" +
            "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
            "<br/><span class=\"remove\"><i class='fa fa-times'></i></span>" +
            "</span>").insertAfter("#files");
          $(".remove").click(function() {
            $(this).parent(".pip").remove();
          });
        });
        fileReader.readAsDataURL(f);
      }
    });
  } else {
    alert("Your browser doesn't support to File API")
  }
});

$(document).on('click', '[name=Reset]', function(){
   $('.pip').remove();
})

The desired outcome is to allow me to set both the file type and size that can be easily changed.

Thanks for any help!

2 Answers 2

1

You can set a condition right after you declare a value to your var f

if(f.size > 200000 || f.type !="image/png"){
   alert("File too big or not a valid Type");
   $("#files").val("");
}

You can also console.log(f); for more properties

Here is my version of your function:

$(document).ready(function() {
    if (window.File && window.FileList && window.FileReader) {
        $("#files").on("change", function(e) {
            var files = e.target.files,
            filesLength = files.length;
            for (var i = 0; i < filesLength; i++) {
                var f = files[i];
                if(f.size > 200000 || f.type !="image/png"){
                    alert("File too big or not a valid Type");
                    $("#files").val("");
                }
                else{
                    var fileReader = new FileReader();
                    fileReader.onload = (function(e) {
                        var file = e.target;
                        $("<span class=\"pip\">" +
                            "<img class=\"imageThumb\" src=\"" + e.target.result + "\" title=\"" + file.name + "\"/>" +
                            "<br/><span class=\"remove\"><i class='fa fa-times'></i></span>" +
                            "</span>").insertAfter("#files");
                        $(".remove").click(function() {
                            $(this).parent(".pip").remove();
                        });

                    });
                    fileReader.readAsDataURL(f);
                }
            }
        });
    } else {
        alert("Your browser doesn't support to File API")
    }
});

$(document).on('click', '[name=Reset]', function(){
    $('.pip').remove();
})
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sorry for my ignorance, but what would the be the syntax for adding more than one file type besides .png? Would it be just the following? if(f.size > 200000 || f.type !="image/png,jpg,pdf,doc,xls"){
I've just added the additional file extensions, but realized that it now requires a message that notifies that the files that are not whitelisted cannot be uploaded. How do I trigger an file type error message?
You might trigger the message by concatenating string with file type variable. Something like this: alert("This file type: "+file.type+" is not allowed");
I'm not very fluent with javascript. Could you show me how to implement your suggestion on a jsfiddle?
1

You can check the size property of the file and the type property of the file.

var maxSize = 20000;//maximum size for the file
var acceptedTypes = ["image/png", "image/jpg"];//accepted image types
if(f.size>maxSize){
    //file is too big
}
if(acceptedTypes.indexOf(f.type)<0){
   //file type is not accepted
}

JSFiddle: https://jsfiddle.net/ordmsw8p/

1 Comment

I would not know how to implement your suggestion. Can you share in on a jsfiddle? I also need the option to upload .docx and .xlsx files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.