HTML5 FileReader替代方案 [英] HTML5 FileReader alternative
问题描述
我需要一些HTML5帮助。我有一个脚本循环遍历所有上传的文件,并获取每个文件的详细信息。目前我正在使用包含FileReader的HTML5技术。 FileReader函数只适用于Chrome和Firefox,所以我正在寻找一种可以在所有其他浏览器中使用的替代方法。
I need some help with HTML5. I have a script that loops through all the uploaded files and gets each file details. Currently I am using HTML5 techniques that include FileReader. The FileReader function only works in Chrome and Firefox, so I am looking for an alternative which will work in all of the other browsers.
我看到Stack Overflow问题 Flash替代FileReader HTML 5 API ,但我无法知道如何使用这个Flash,并且没有任何其他解决方案,因此我可以遍历所有上传的文件并获取每个文件的详细信息(这些信息可以在Safari和Internet Explorer中使用)?
I saw the Stack Overflow question Flash alternative for FileReader HTML 5 API, but I wasn't able to figure how to use this Flash thing, and aren't there any other solutions so I can loop through all of the uploaded files and get each file details (which will work in Safari and Internet Explorer)?
推荐答案
完全不使用FileReader,而是通过event.files循环,并通过files [i]和发送一个由XHR与FormData对象的AJAX请求(为我工作,因为我决定我不需要获取文件数据):
Ended up not using FileReader at all, instead I looped through event.files and got each file by files[i] and sent an AJAX request by XHR with a FormData object (worked for me because I decided I don't need to get the file data):
var xhrPool = {};
var dt = e.dataTransfer;
var files = (e.files || dt.files);
for (var i = 0; i < files.length; i++) {
var file = files[i];
// more code...
xhrPool[i] = getXMLHttpRequest();
xhrPool[i].upload.onprogress = uploadProgress;
initXHRRequest(xhrPool[i], i, file);
data = initFormData(i, file);
xhrPool[i].send(data);
}
function initFormData(uploaded, file) {
var data = new FormData();
data.append(uploaded, file);
// parameters...
return data;
}
function uploadProgress() {
// code..
}
function initXHRRequest(xhr, uploaded, file) {
// code... onreadystatechange...
xhr.open("POST", "ajax/upload.php");
xhr.setRequestHeader("X-File-Name", file.name);
}
function getXMLHttpRequest()
{
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch(ex) {
return null;
}
}
}
这篇关于HTML5 FileReader替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!