javascript - React:使用 JSZip 从项目目录中读取 Zip 文件
问题描述
我正在尝试从 React 的项目目录中读取包含图像的 zip 文件。
当我从 中打开 Zip 文件时<input type="file" />
,它通过获取event.target.files[0]
. 但是当我将同一个文件放入反应项目中,然后尝试通过提供路径打开它时,它不起作用。
示例:“./test.zip”
我当前的代码:
let jsZip = new JSZip();
jsZip.loadAsync("./test.zip").then(function (zip) {
let imagess = [];
Object.keys(zip.files).forEach(function (filename) {
zip.files[filename].async("base64").then(function (fileData) {
const image = document.createElement("img");
image.src = "data:image/*;base64," + fileData;
document.querySelector(".unziped-container").appendChild(image);
});
});
});
我已经坚持了几个小时,文档也没有帮助。任何帮助表示赞赏。
解决方案
Anyone coming across this can use JSZip-utils and write the following code
JSZipUtils.getBinaryContent("../path/file.zip", function (err, data) {
if (err) {
throw err;
}
const jsZip = new JSZip();
jsZip.loadAsync(data).then(function (zip) {
Object.keys(zip.files).forEach(function (filename) {
zip.files[filename].async("base64").then(function (fileData) {
const image = document.createElement("img");
image.src = "data:image/*;base64," + fileData;
const unziped = document.querySelector(".unziped-container");
unziped.appendChild(image);
});
});
});
});
推荐阅读
- amazon-web-services - 在 AWS-API-Gateway 中通过 CLI 将集成附加到路由
- python - 检查字符串是否包含特定数量的字母和数字
- javascript - react 和 next.js 视差
- java - 如何在Java中交叉迭代两个arrayList?
- c++ - Qt QML - QModBus 读取被 QML BusyIndicator/Animation 损坏 - SingleThread
- reactjs - firestore Geopoints 为什么不能将类调用为函数错误
- visual-studio-code - Visual Studio Code:关闭后重新打开远程工作区
- java - 从链表中删除重复的元素(整数类型)
- oracle - FDA查询下发生了什么?
- python - SQL Server命令以json形式返回查询结果,不适用于python