Zah*_*eed 16 javascript synchronous filereader promise
我有以下HTML代码:
<input type='file' multiple>
Run Code Online (Sandbox Code Playgroud)
这是我的JS代码:
var inputFiles = document.getElementsByTagName("input")[0];
inputFiles.onchange = function(){
var fr = new FileReader();
for(var i = 0; i < inputFiles.files.length; i++){
fr.onload = function(){
console.log(i) // Prints "0, 3, 2, 1" in case of 4 chosen files
}
}
fr.readAsDataURL(inputFiles.files[i]);
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何使这个循环同步?首先等待文件完成加载然后转到下一个文件.有人告诉我使用__CODE__.但我不能让它发挥作用.这是我正在尝试的:
var inputFiles = document.getElementsByTagName("input")[0];
inputFiles.onchange = function(){
for(var i = 0; i < inputFiles.files.length; i++){
var fr = new FileReader();
var test = new Promise(function(resolve, reject){
console.log(i) // Prints 0, 1, 2, 3 just as expected
resolve(fr.readAsDataURL(inputFiles.files[i]));
});
test.then(function(){
fr.onload = function(){
console.log(i); // Prints only 3
}
});
};
}
Run Code Online (Sandbox Code Playgroud)
提前致谢...
mid*_*ido 26
如果你想使用Promises顺序(不同步),你可以这样做:
var inputFiles = document.getElementsByTagName("input")[0];
inputFiles.onchange = function(){
var promise = Promise.resolve();
inputFiles.files.map( file => promise.then(()=> pFileReader(file)));
promise.then(() => console.log('all done...'));
}
function pFileReader(file){
return new Promise((resolve, reject) => {
var fr = new FileReader();
fr.onload = resolve; // CHANGE to whatever function you want which would eventually call resolve
fr.readAsDataURL(file);
});
}
Run Code Online (Sandbox Code Playgroud)
小智 15
我们修改了midos的答案以使其工作如下:
function readFile(file){
return new Promise((resolve, reject) => {
var fr = new FileReader();
fr.onload = () => {
resolve(fr.result )
};
fr.readAsText(file.blob);
});
}
Run Code Online (Sandbox Code Playgroud)
我通过添加工作示例来升级Jens Lincke 答案并引入 async/await 语法
function readFile(file) {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.onload = x=> resolve(fr.result);
fr.onerrror = reject;
fr.readAsDataURL(file) // or readAsText(file) to get raw content
})}
Run Code Online (Sandbox Code Playgroud)
function readFile(file) {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.onload = x=> resolve(fr.result);
fr.onerrror = reject;
fr.readAsDataURL(file) // or readAsText(file) to get raw content
})}
Run Code Online (Sandbox Code Playgroud)
function readFile(file) {
return new Promise((resolve, reject) => {
let fr = new FileReader();
fr.onload = x=> resolve(fr.result);
fr.readAsDataURL(file) // or readAsText(file) to get raw content
})}
async function load(e) {
for(let [i,f] of [...e.target.files].entries() ){
msg.innerHTML += `<h1>File ${i}: ${f.name}</h1>`;
let p = document.createElement("pre");
p.innerText += await readFile(f);
msg.appendChild(p);
}
}Run Code Online (Sandbox Code Playgroud)
性质FileReader是你无法使其操作同步.
我怀疑你并不真正需要或希望它是同步的,只是你想要正确获得结果URL.如果是这样,我不认为承诺会真的有帮助.相反,只需跟踪您有多少未完成的操作,以便在完成后知道:
var inputFiles = document.getElementsByTagName("input")[0];
inputFiles.onchange = function(){
var data = []; // The results
var pending = 0; // How many outstanding operations we have
// Schedule reading all the files (this finishes before the first onload
// callback is allowed to be executed)
Array.prototype.forEach.call(inputFiles.files, function(file, index) {
// Read this file, remember it in `data` using the same index
// as the file entry
var fr = new FileReader();
fr.onload = function() {
data[index] = fr.result;
--pending;
if (pending == 0) {
// All requests are complete, you're done
}
}
fr.readAsDataURL(file);
++pending;
});
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您由于某种原因要按顺序(但仍然是异步)读取文件,则可以通过仅在前一个调用完成时调度下一个调用来执行此操作:
// Note: This assumes there is at least one file, if that
// assumption isn't valid, you'll need to add an up-front check
var inputFiles = document.getElementsByTagName("input")[0];
inputFiles.onchange = function(){
var index = 0;
readNext();
function readNext() {
var file = inputFiles.files[index++];
var fr = new FileReader();
fr.onload = function() {
// use fr.result here
if (index < inputFiles.files.length) {
// More to do, start loading the next one
readNext();
}
}
fr.readAsDataURL(file);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25760 次 |
| 最近记录: |