wil*_*ber 34
这绝对是现实的,可以不使用任何第三方插件.
以下片段应该让您了解它是如何工作的:
掉落区域
$(".drop-files-container").bind("drop", function(e) {
var files = e.originalEvent.dataTransfer.files;
processFileUpload(files);
// forward the file object to your ajax upload method
return false;
});
Run Code Online (Sandbox Code Playgroud)
processFileUpload() - 方法:
function processFileUpload(droppedFiles) {
// add your files to the regular upload form
var uploadFormData = new FormData($("#yourregularuploadformId")[0]);
if(droppedFiles.length > 0) { // checks if any files were dropped
for(var f = 0; f < droppedFiles.length; f++) { // for-loop for each file dropped
uploadFormData.append("files[]",droppedFiles[f]); // adding every file to the form so you could upload multiple files
}
}
// the final ajax call
$.ajax({
url : "upload.php", // use your target
type : "POST",
data : uploadFormData,
cache : false,
contentType : false,
processData : false,
success : function(ret) {
// callback function
}
});
}
Run Code Online (Sandbox Code Playgroud)
形式的例子
<form enctype="multipart/form-data" id="yourregularuploadformId">
<input type="file" name="files[]" multiple="multiple">
</form>
Run Code Online (Sandbox Code Playgroud)
随意使用这样的东西作为起点.您可以在此处找到浏览器支持http://caniuse.com/#feat=xhr2
当然,你可以添加你想要的任何额外的东西,如进度条,预览,动画......