学习 js 中,不知道怎么改,google 也查不到相关的。。。多谢
$(function() {
$("body").bind("dragover", function(ev) {
ev.preventDefault();
//console.log("dragover event was prevented");
});
$("body").bind("drop", function(ev) {
ev.preventDefault();
//console.log("drop event was prevented");
var files = ev.originalEvent.dataTransfer.files;
//console.log(files);
for (file of files) {
//console.log(`${file.name} ${file.size} bytes`);
if (!file.size) {
console.log(`error: ${file.name} is an empty file`);
continue;
}
if (file.type.indexOf("image") == -1) {
console.log(`error: ${file.name} is a non-image file`);
continue;
}
var fd = new FormData();
fd.append("pic", file);
//console.log(`uploading ${file.name} ...`);
$.ajax({
url: "https://xxxx.com/upload.php",
type: "POST",
data: fd,
processData: false,
contentType: false,
success: function(res) {
console.log(res);
},
error: function(jqXHR) {
console.log(jqXHR.responseText);
},
xhr: function() {
myXHR = $.ajaxSettings.xhr();
myXHR.upload.addEventListener("progress", function(e) {
console.log(`${file.name} uploaded ${e.loaded}`);
});
return myXHR;
}
});
}
});
});
2
hard2reg OP 不用了。。还可以 edit。。。
|
3
hard2reg OP php 开头已设置 Access-Control-Allow-Origin: *
|
4
leonlu 2017-09-06 08:08:08 +08:00 via iPhone
如果跨域了,chrome 是会先发个 options 问问能不能跨域的
|
5
hard2reg OP |
6
Kokororin 2017-09-06 08:52:09 +08:00 via iPhone
append 的第三个参数就是 filename
|
7
hard2reg OP |
11
hard2reg OP @Kokororin 看了 Mozilla 的文档了解了一下 let,好像闭包传参的兼容性和可读性更好?
|
12
Kokororin 2017-09-06 12:50:54 +08:00
@hard2reg #11 http://caniuse.com/#search=let 可读性难道不是 let 更好吗,兼容性不是问题,过一下 babel
|
14
hard2reg OP @Kokororin 试了下文件名正常输出了,十分感谢!
``` jqXHR.upload.addEventListener("progress", function(ev) { console.log(`ev.total ${ev.total} Size ${file.size}`); }); ``` 输出结果显示:ev.total 获取到的字节数比 file.size 获取到的大一些,正常吗? |