Javascript 获取zip文件的内容
下面的Javascript 获取zip文件的内容,javascript,typescript,axios,fetch,jszip,Javascript,Typescript,Axios,Fetch,Jszip,下面的URL指向一个zip文件,其中包含一个名为bundlesizes.json的文件。我正在尝试读取我的React应用程序中的json文件的内容(不涉及节点服务器/后端) 通过执行以下操作,我能够获得zip文件的内容 const url = 'https://dev.azure.com/uifabric/cd9e4e13-b8db-429a-9c21-499bf1c98639/_apis/build/builds/8838/artifacts?artifactName=drop&a
URL
指向一个zip文件,其中包含一个名为bundlesizes.json
的文件。我正在尝试读取我的React
应用程序中的json
文件的内容(不涉及节点
服务器/后端)
通过执行以下操作,我能够获得zip
文件的内容
const url =
'https://dev.azure.com/uifabric/cd9e4e13-b8db-429a-9c21-499bf1c98639/_apis/build/builds/8838/artifacts?artifactName=drop&api-version=4.1&%24format=zip';
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
});
console.log(response.data);
这将发出zip文件(非ascii
字符)。但是,我希望读取其中的bundlesizes.json
文件的内容
为此,我查阅了jszip
,并尝试了以下方法:
var zip = new JSZip();
zip.createReader(
new zip.BlobReader(response.data),
function(reader: any) {
// get all entries from the zip
reader.getEntries(function(entries: any) {
if (entries.length) {
// get first entry content as text
entries[0].getData(
new zip.TextWriter(),
function(text: any) {
// text contains the entry data as a String
console.log(text);
// close the zip reader
reader.close(function() {
// onclose callback
});
},
function(current: any, total: any) {
// onprogress callback
console.log(current);
console.log(total);
}
);
}
});
},
function(error: any) {
// onerror callback
console.log(error);
}
);
但是,这对我来说不起作用,并且会出错。
这是我收到的错误
如何使用Javascript
/Typescript
在React
应用程序中读取zip中的文件内容
var zip = new JSZip();
zip.createReader(
new zip.BlobReader(response.data),
function(reader: any) {
// get all entries from the zip
reader.getEntries(function(entries: any) {
if (entries.length) {
// get first entry content as text
entries[0].getData(
new zip.TextWriter(),
function(text: any) {
// text contains the entry data as a String
console.log(text);
// close the zip reader
reader.close(function() {
// onclose callback
});
},
function(current: any, total: any) {
// onprogress callback
console.log(current);
console.log(total);
}
);
}
});
},
function(error: any) {
// onerror callback
console.log(error);
}
);