jquery - 使用 JSZIP 库创建实时图像 src 的 zip 的问题
问题描述
我正在使用 JSZIP 库创建一个 zip 文件。为此,我编写了如下所示的代码,其中src
给出了图像。在我的站点中,它是我的实时站点路径。该 zip 文件已正确创建。
问题是它正在创建这样的文件夹结构:
zipFilename->https:->website.com->wp-content->themes->theamename->templates->images->imageName
我想要这样的文件夹结构:
zipFilename->imageName or zipFilename->images->imageName
任何人都可以帮我解决这个问题
function downloadAll() {
var urls = [];
$(".image").each(function(index) {
urls.push($(this).attr('src'));
});
var zip = new JSZip();
var count = 0;
var zipFilename = "zipFilename.zip";
urls.forEach(function(url) {
var filename = "filename";
JSZipUtils.getBinaryContent(url, function(err, data) {
if (err) {
throw err; // or handle the error
}
zip.file(url, data, {
binary: true
});
count++;
if (count == urls.length) {
zip.generateAsync({
type: "blob"
})
.then(function(blob) {
saveAs(blob, zipFilename);
});
}
});
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.1/jszip.min.js"></script>
<div class="filename">
<div class="comresult col-sm-12">
<img src="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" class="image" width="100PX" height="100PX">
<b class="name">Google</b>
<a href="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" download="" id="download" class="btn" style="">DOWNLOAD</a>
<img src="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" class="image" width="100PX" height="100PX">
<b class="name">Google</b>
<a href="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" download="" id="download" class="btn" style="">DOWNLOAD</a>
<img src="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" class="image" width="100PX" height="100PX">
<b class="name">Google</b>
<a href="http://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/alien_1_1.png" download="" id="download" class="btn" style="">DOWNLOAD</a>
</div>
<div class="downloadall col-sm-12">
<button onclick="downloadAll()" class="directdownload">
<img width="20px" src="<?php echo get_template_directory_uri(); ?>/assets/images/download.png" alt="Download" style="margin-right: 10px; ">Download All!
</button>
</div>
</div>
解决方案
我有用
urls.forEach(function (url, i) {
var filename = urls[i];
filename = filename.replace(/[\/\*\|\:\<\>\?\"\\]/gi, '').replace("zipFilenamehttpswebsite.comwp-contentthemestheamenametemplatesimages","");
// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(url, function (err, data) {
if (err) {
throw err; // or handle the error
}
zip.file(filename, data, { binary: true });
count++;
if (count == urls.length) {
zip.generateAsync({ type: 'blob' }).then(function (content) {
saveAs(content, zipFilename);
});
}
});
});
它工作正常
推荐阅读
- entity-framework-core - EF Core 5:必须是可简化节点
- azure - Azure DevOps Starter Wizard 中缺少资源组字段
- php - php 7.4 mongodb dll 文件(1.9.0) 与 windows 10 系统上的 php 8 不兼容
- angular11 - 使用 HttpClient 的 Angular 11 中的 Cors 问题
- javascript - 如何在 react 中使用 formik 创建动态下拉列表?
- php - 创建排行榜
- vue.js - 上下文和凭据的好处
- node.js - 生产环境中的 MongoDB 备份和恢复
- git - 如何使用 android studio 删除我在 gitlab 上的最后一次推送
- node.js - 做一个从 nodejs 到 oracle 数据库的测试连接