【发布时间】:2019-12-26 15:41:42
【问题描述】:
你好,节日快乐!!
我需要关于如何从生成 zipfile 到我的 vuejs 组件中的外部函数获取数据的建议,以便为 JSZip 插件创建进度条:https://stuk.github.io/jszip/documentation/api_jszip/generate_async.html
我导入我的文件: 从“@/utils/downloader.js”导入{ generateZIP};
并在 vuejs 中通过按钮触发的方法调用它:
<template>
...
<div v-for="result of results" :key="result.item.refID">
<section class="row" @click="selectByRow(result.item)">
<input
type="checkbox"
:id="result.item.refID"
:value="result.item.refID"
v-model="checkedItems"
class="checkbox"
/>
</div>
<!-- FOUND RESULTS -->
<div class="name">{{ result.item.marketingName }}</div>
</section>
</div>
<!-- Download all checked items -->
<div>
<button
v-if="checkedItems.length > 1"
@click="downloadAll(checkedItems)"
class="button"
>
Download Selection
</button>
</template>
...
<script>
import { mapState, mapActions, mapMutations, mapGetters } from "vuex";
import { generateZIP } from "@/utils/downloader.js";
...
export default {
data() {
return {
// Path to point to pictures storage
domainName: this.$domainName,
// Array to gather search results
searchArray: [],
checkedItems: [],
// make a special array for row selection
checkedRow: []
};
},
methods:
downloadAll(files) {
// Prepare path
const fullPath = `${this.domainName}/files/${this.reqPath}/`;
const filesArray = [];
files.forEach(fileID => {
let obj = this.results.find(value => value.item.refID == fileID);
if (obj.item.images !== undefined) {
filesArray.push(obj.item.images);
}
});
generateZIP(filesArray.flat(), fullPath);
},
selectByRow(resultID) {
// Check if select resultID.refID is already in checkedItems and store it in variable if its present.
const isInArray = this.checkedItems.find(name => name === resultID.refID);
// if the ref not in array, add it
if (!isInArray) {
this.checkedItems.push(resultID.refID);
// Add checkedRow full information object
this.checkedRow.push(resultID);
} else {
// if already in array, remove it
this.checkedItems = this.checkedItems.filter(
name => name !== resultID.refID
);
this.checkedRow = this.checkedRow.filter(
name => name.refID !== resultID.refID
);
}
...
一切正常,现在我添加一些显示压缩进度的反馈。我将在 downloader.js 中调用一个可用的回调函数“updateCallback”
zip.generateAsync({type:"blob"}, function updateCallback(metadata) {
console.log("progression: " + metadata.percent.toFixed(2) + " %");
if(metadata.currentFile) {
console.log("current file = " + metadata.currentFile);
}
})
...
export {
generateZIP
}
很酷,它会在我的控制台日志中显示进度。
但是我如何将这个元数据对象导入 vue 以在我的应用程序中显示它?
非常感谢!
【问题讨论】:
-
您能否添加一些有关您已有的 (Vue) 代码的更多信息?
-
我添加了完整的方法代码,vuefile很大我不想污染。 “文件”数组是与图片 url、它们的名称和相对路径相对应的对象列表。我从我的下载器下载这些图片并将它们收集在一个 zip 中。
-
您可以删除代码,使其仅包含与您的问题相关的部分,有关更多信息,请参阅this link。
标签: javascript vue.js jszip