html filereader 压缩,javascript - Opening zip files in browser with FileReader and JSZip.js - Stack Ov...

作者分享了如何在网页上利用FileReader和JSZip.js打开ZIP文件,并解决将FileReader对象传递给JSZip时遇到的TypeError。文章详细探讨了可能的错误来源及如何正确运用loadAsync方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm trying to open up zip files inside the browser with FileReader and JSZip.js, then handle the files contained inside. I can't figure out how to correctly pass the FileReader object to JSZip.

Here's a stripped version of the page I use to load the javascript:

And the javascript in zipscan.js after removing all unnecessary code:

function checkFiles()

{

//Check support for the File API support

if ( window.File && window.FileReader && window.FileList && window.Blob )

{

var fileSelected = document.getElementById( "inputFile" );

fileSelected.addEventListener( "change", handleFile, false );

}

else

{

alert( "Files are not supported" );

}

}

function handleFile( evt )

{

//Set wanted file object

var fileToRead = evt.target.files[0];

//Create fileReader object

var fileReader = new FileReader();

fileReader.onload = function ( e )

{

//Create JSZip instance

var archive = new JSZip().loadAsync( e.target );

//Testing that it is loaded correctly

alert( e.target );

alert( archive.file( "hello.txt" ).name );

}

fileReader.readAsArrayBuffer( fileToRead );

}

window.addEventListener( "load", checkFiles, false );

The first alert displays [object FileReader], and the second results in a TypeError, archive.file(...) is being null.

I have used FileReader's readAsText method with the same code to open text files successfully, so if there is an error it's either in using readAsArrayBuffer (JSZip documentation suggested it) or in the way I'm using it. Almost all the resources I've found about JSZip use the old method with constructor parameters instead of loadAsync so it could be I'm not using it right.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值