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

Javascript 如何限制用户添加重复项和特定文件格式,javascript,jquery,Javascript,Jquery,如何实现gmail风格的文件上传。从下面的代码中,我可以选择多个文件,并显示带有锚定标记的选定文件。如何避免重复文件和添加,以及如何限制用户仅选择pdf和doc文件?如果用户选择“复制”或用户选择的不是doc或pdf,我需要发出警报 <div class="container"> <form id="fileupload" action="#" method="POST" enctype="multipart/form-data"> <d

如何实现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();
    });
$.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();
});