我在Safari中使用HTML 5拖放功能会产生非常奇怪的行为。
我的代码看起来像这样,没什么特别的:
handleFiles = function (files, e) {
// Traverse throught all files and check if uploaded file type is image
var imageType = /image.*/;
var file = files[0];
// check file type
if (!file.type.match(imageType)) {
alert("File \"" + file.name + "\" is not a valid image file. ");
return false;
}
// check file size
if (parseInt(file.size / 1024) > 2050) {
alert("File \"" + file.name + "\" is too big.");
return false;
}
uploadFile(file);
};
uploadFile = function (file) {
xhr = new XMLHttpRequest();
xhr.open("post", "fileuploadhandler.ext", true);
xhr.upload.addEventListener("progress", function (event) {
if (event.lengthComputable) {
$("#progress").css("width", (event.loaded / event.total) * 100 + "%");
$(".percents").html(" " + ((event.loaded / event.total) * 100).toFixed() + "%");
$(".up-done").html((parseInt(event.loaded / 1024)).toFixed(0));
} else {
alert("Failed to compute file upload length");
}
}, false);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
logAction(oEvent);
$("#progress").css("width", "100%");
$(".percents").html("100%");
$(".up-done").html((parseInt(file.size / 1024)).toFixed(0));
fileUploadedComplete(true, oEvent);
} else {
fileUploadedComplete(false, oEvent);
alert("Error" + xhr.statusText);
}
}
};
// Set headers
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.setRequestHeader("X-File-Name", file.name);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("X-File-Type", file.type);
// Send the file (doh)
xhr.send(file);
};
这适用于Chrome和Firefox,
任何帮助将不胜感激!
最佳,
tribe84
答案 0 :(得分:0)
我无法在Safari for OS X上复制您的错误。
也许这个问题与Windows上的Safari有关。可能不是在Windows上测试Safari的最佳方法。
这是一个有效的JSFiddle:http://jsfiddle.net/wh3E5/