对于截取读入的文件,一定要new FileReader,不可写全局调用同一个reader.
错误代码!!!
function readAsBinaryString(file,callback){ readrSpt(); var part= 0,fileName=file.name, step=1024*10,start= 0, a=0,b= 0,total=file.size,loaded= 0,reader=new FileRead(),fileType=file.type;///这样写是错的,将导致reader流不在是同步读取 //$("#fileLink").attr("href",URL.createObjectURL(blob)); //将文件以arraybuffer形式读入 (function(start){ var blob=file.slice(start,start+step+1),self=arguments.callee;// console.log(blob) reader.readAsArrayBuffer(blob); reader.οnprοgress=function(e){ loaded+=e.loaded; var per=loaded/total; if(loaded==total){ per=100; callback(this.result,part,fileName,fileType,true); }else{ per*=100; callback(this.result,part); part++; } } reader.οnlοad=function(e){ console.log(loaded) if(loaded<total){ self(loaded); // callback(this.result); }else{ loaded=total; } } })(start); }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
正确代码
function readAsBinaryString(file,callback){ readrSpt(); var part= 0,fileName=file.name, step=1024*10,start= 0, a=0,b= 0,total=file.size,loaded= 0,fileType=file.type; //$("#fileLink").attr("href",URL.createObjectURL(blob)); //将文件以arraybuffer形式读入 (function(start){ var blob=file.slice(start,start+step+1),reader = new FileReader(),self=arguments.callee; // console.log(blob) reader.readAsArrayBuffer(blob); reader.οnprοgress=function(e){ loaded+=e.loaded; var per=loaded/total; if(loaded==total){ per=100; callback(this.result,part,fileName,fileType,true); }else{ per*=100; callback(this.result,part); part++; } } reader.οnlοad=function(e){ console.log(loaded) if(loaded<total){ self(loaded); // callback(this.result); }else{ loaded=total; } } })(start); }
转载来源:http://www.cnblogs.com/tom-chang/p/4060650.html