I'm trying to create a download button which will allow the user to quickly download all assets in an entries image gallery.
I've started by adding each image url into an array stored in a data attr of the button.
<button data-zip-download='[{% for imageUrl in entry.collectionImages %}"{{imageUrl.url}}"{% if not loop.last %},{% endif %}{% endfor %}]'>Download Gallery</button>
I've then tried to get these URL's using jQuery, and zipping/downloading the zip with JSZip.js and FileSaver.js. Although through a bit of research I think I've found this is generally achieved through PHP.
I'm wondering if anyone on here has had a similar situation and has an idea of how I can achieve this.
I'm not too experienced with ajax but have also read that ajax is necessary to get this working via jQuery.
So far my JavaScript looks as so:
$('[data-zip-download]').each(function(){
var $this = $(this),
urls = $this.data('zip-download'),
files = [],
zip = new JSZip();
for (var i = 0; i < urls.length; i++) {
files.push(urls[i]);
};
zip.file(files);
$this.on('click', function(){
zip.generateAsync({type:"blob"}).then(function (blob) { // 1) generate the zip file
saveAs(blob, "gallery.zip"); // 2) trigger the download
}, function (err) {
$this.text(err);
});
});
});
This however only creates a corrupted zip file as I imagine I need to actually load those images via ajax.
I suppose it would be ideal to simply use a craft/twig function if such a thing exists, although anything that works would be great haha.
Sorry for not having much knowledge on this specific need, really hoping someone out there can help!
Thanks!