如何限制用户添加重复项和特定文件格式

时间:2018-02-06 10:17:17

标签: javascript jquery

如何实现gmail样式的文件上传。从下面的代码我可以选择多个文件,并显示带有锚标记的选定文件。如何避免重复文件和添加以及如何限制用户仅选择pdf和doc文件?如果用户选择重复或者如果用户选择doc或pdf以外的其他用户,我需要发出警报

  
<div class="container">

    <form id="fileupload" action="#" method="POST" enctype="multipart/form-data">

        <div class="row files" id="files1">
            <h2>Files 1</h2>

                Browse  <input type="file" name="files1" multiple />

            <br />
            <ul class="fileList"></ul>
        </div>
        </form>
</div>
     var filesToUpload = [];
var output = [];
$.fn.fileUploader = function (filesToUpload) {
    this.closest(".files").change(function (evt) {

        for (var i = 0; i < evt.target.files.length; i++) {
        var found = false;
        for(var j = 0; j < filesToUpload.length; j++){
          if(evt.target.files[i].name == filesToUpload[j].name){
              found = true;
              break; 
               }
           }
           if(found == false){
   filesToUpload.push(evt.target.files[i]);
  }
  else{
  alert("duplicate");
  }
        }

        for (var i = 0; i < evt.target.files.length; i++) { 
        var file= evt.target.files[i];      
                var found = false;
        for(var j = 0; j < filesToUpload.length; j++){
          if(evt.target.files[i].name == filesToUpload[j].name){
              found = true;
              break; 
               }
           }   

           if(found == false){   
            var removeLink = "<a class=\"removeFile\" href=\"#\" data-fileid=\"" + i + "\">X</a>";
            output.push("&nbsp&nbsp&nbsp<li><strong>",file.name,"</strong>&nbsp",removeLink,"</li> ");
  }
  else{
  alert("duplicate");
  } 
            }

        $(this).children(".fileList").append(output.join(""));
    });
};



$(document).on("click",".removeFile", function(e){
    e.preventDefault();
    var fileName = $(this).parent().children("strong").text();
     // loop through the files array and check if the name of that file matches FileName
    // and get the index of the match
    for(i = 0; i < filesToUpload.length; ++ i){
        if(filesToUpload[i].name == fileName){
            //console.log("match at: " + i);
            // remove the one element at the index where we get a match
            filesToUpload.splice(i, 1);
        }   
    }
    //console.log(filesToUpload);
    // remove the <li> element of the removed file from the page DOM
    $(this).parent().remove();
});


    $("#files1").fileUploader(filesToUpload);
    $("#files2").fileUploader(filesToUpload);

    $("#uploadBtn").click(function (e) {
        e.preventDefault();
    });

1 个答案:

答案 0 :(得分:1)

$.fn.fileUploader = function(filesToUpload) {
    this.closest(".files").change(function(evt) {
                        var index;
                        for (var i = 0; i < evt.target.files.length; i++) {
                            index = filesToUpload.indexOf(evt.target.files[i]);
                            if (index > -1) {
                                filesToUpload.splice(index, 1);
                            }
                        }
                        for (i = 0; i < evt.target.files.length; i++) {
                            var filename = evt.target.files[i].name;
                            var valid_extensions = /(\.pdf|\.doc|\.docx)$/i;                            
                            if (valid_extensions.test(filename)) {
                                for (var i = 0; i < evt.target.files.length; i++) {
                                    filesToUpload.push(evt.target.files[i]);
                                };
                                var output = [];
                                for (var i = 0, f; f = evt.target.files[i]; i++) {
                                    var removeLink = "<a class=\"removeFile\" href=\"#\" data-fileid=\""+ i + "\">Remove</a>";
                                    output.push("<li><strong>", escape(f.name),"</strong> - ", removeLink,"</li> ");
                                }
                                $(this).children(".fileList").append(output.join(""));
                            } else {
                                alert('Invalid File');
                                return false;
                            }
                        }
                    });
};

var filesToUpload = [];
$(document).on("click", ".removeFile", function(e) {
    e.preventDefault();
    var fileName = $(this).parent().children("strong").text();
    // loop through the files array and check if the name of that file matches
    // FileName
    // and get the index of the match
    for (i = 0; i < filesToUpload.length; ++i) {
        if (filesToUpload[i].name == fileName) {
            // console.log("match at: " + i);
            // remove the one element at the index where we get a match
            filesToUpload.splice(i, 1);
        }
    }
    // console.log(filesToUpload);
    // remove the <li> element of the removed file from the page DOM
    $(this).parent().remove();
});

$("#files1").fileUploader(filesToUpload);
$("#files2").fileUploader(filesToUpload);

$("#uploadBtn").click(function(e) {
    e.preventDefault();
});