admin管理员组文章数量:1334825
I am trying to zip two files from an URL using the jszip add on and I am having a little issue. I am trying to zip two files from a url(testing with imgur links currently), however only one of my files is being zipped. I am not sure if it is something I am doing wrong in my foreach function?
Any suggestions would be awesome thank you.
function urlToPromise(url)
{
return new Promise(function(resolve, reject)
{
JSZipUtils.getBinaryContent(url, function (err, data)
{
if(err)
{
reject(err);
} else {
resolve(data);
}
});
});
}
(function ()
{
var zip = new JSZip();
var count = 0;
var zipFilename = "instasamplePack.zip";
var urls = [
'.png',
'.png'
];
function bindEvent(el, eventName, eventHandler) {
if (el.addEventListener){
// standard way
el.addEventListener(eventName, eventHandler, false);
} else if (el.attachEvent){
// old IE
el.attachEvent('on'+eventName, eventHandler);
}
}
// Blob
var blobLink = document.getElementById('kick');
if (JSZip.support.blob) {
function downloadWithBlob() {
urls.forEach(function(url){
var filename = "element" + count + ".png";
// 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, urlToPromise(urls[count]), {binary:true});
count++;
if (count == urls.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipFilename);
});
}
});
});
}
bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
})();
I am trying to zip two files from an URL using the jszip add on and I am having a little issue. I am trying to zip two files from a url(testing with imgur links currently), however only one of my files is being zipped. I am not sure if it is something I am doing wrong in my foreach function?
Any suggestions would be awesome thank you.
function urlToPromise(url)
{
return new Promise(function(resolve, reject)
{
JSZipUtils.getBinaryContent(url, function (err, data)
{
if(err)
{
reject(err);
} else {
resolve(data);
}
});
});
}
(function ()
{
var zip = new JSZip();
var count = 0;
var zipFilename = "instasamplePack.zip";
var urls = [
'https://i.imgur./blmxryl.png',
'https://i.imgur./Ww8tzqd.png'
];
function bindEvent(el, eventName, eventHandler) {
if (el.addEventListener){
// standard way
el.addEventListener(eventName, eventHandler, false);
} else if (el.attachEvent){
// old IE
el.attachEvent('on'+eventName, eventHandler);
}
}
// Blob
var blobLink = document.getElementById('kick');
if (JSZip.support.blob) {
function downloadWithBlob() {
urls.forEach(function(url){
var filename = "element" + count + ".png";
// 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, urlToPromise(urls[count]), {binary:true});
count++;
if (count == urls.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipFilename);
});
}
});
});
}
bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
})();
Share
Improve this question
edited Jan 18, 2017 at 18:15
David Duponchel
4,0693 gold badges30 silver badges36 bronze badges
asked Jan 18, 2017 at 1:46
Nick GarverNick Garver
5471 gold badge6 silver badges19 bronze badges
0
1 Answer
Reset to default 6When you do
urls.forEach(function(url){
var filename = "element" + count + ".png"; // 1
JSZipUtils.getBinaryContent(url, function (err, data) {
count++; // 2
});
});
you execute 1
two times and when a download finish you call 2
. count
is still zero in both cases (at 1
), you overwrite one image with the other (same name).
You also download each image two times: urlToPromise
already calls JSZipUtils.getBinaryContent
.
To fix that:
- use the index parameter of the forEach callback instead of
count
- JSZip accepts promises (and internally wait for them),
urlToPromise
already convert everything: use it - don't try to wait for promises, JSZip already does that
This gives a new downloadWithBlob
function:
function downloadWithBlob() {
urls.forEach(function(url, index){
var filename = "element" + index + ".png";
zip.file(filename, urlToPromise(url), {binary:true});
});
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipFilename);
});
}
本文标签: javascriptjszip only zipping one of the two files from urlStack Overflow
版权声明:本文标题:javascript - jszip only zipping one of the two files from url - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742272121a2444502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论