在没有库的jquery中拖放文件夹上传

时间:2016-01-29 09:00:01

标签: javascript jquery ajax html5 jquery-file-upload

我已经写了正常的多文件拖放上传器,

HTML:

<div class="drop-box drop-area">
    <form enctype="multipart/form-data" id="yourregularuploadformId">
        <input type="file" name="files[]" multiple="multiple" class="hidden">        
    </form>
</div>

SCRIPT:

$('.drop-area').on(
            'drop',
            function (e) {
                e.preventDefault();
                e.stopPropagation();
                if (e.originalEvent.dataTransfer) {
                    if (e.originalEvent.dataTransfer.files.length) {
                     processFileUpload(e.originalEvent.dataTransfer.files);
                    }
                }
            }
    );

function processFileUpload(droppedFiles) {
        var uploadFormData = new FormData($("#yourregularuploadformId")[0]);
        if (droppedFiles.length > 0) { 
            for (var f = 0; f < droppedFiles.length; f++) { 
                uploadFormData.append("files[]", droppedFiles[f]); 
            }
        }

$.ajax({
 url: "files.php",
            type: "POST",
            data: uploadFormData,
            cache: false,
            contentType: false,
            processData: false,
            success: function (ret) {
               alert("file uploaded !");
            },
});
}

我必须上传文件夹,嵌套文件夹(dir)。这该怎么做 ?任何人都有这个。它是可能的。我不需要使用库来做这件事。 (它必须适用于IE10)

1 个答案:

答案 0 :(得分:1)

对于 Chrome&gt; = 21 ,它将有效Chrome 21 Drag and Drop