jQuery文件上传大小和类型限制

时间:2018-10-26 22:29:48

标签: javascript jquery twitter-bootstrap-3 jquery-file-upload multifile-uploader

我有一个JQuery多文件上传解决方案,该解决方案要求限制文件格式和大小。

这里是JSFIDDLE,下面是JS代码:

$(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();
})

期望的结果是允许我设置可以轻松更改的文件类型和大小。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以在为var f声明值后立即设置条件

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

您也可以console.log(f);更多属性

这是我的函数版本:

$(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();
})

答案 1 :(得分:1)

您可以检查文件的size属性和文件的type属性。

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/