Closed iwe-hi closed 2 years ago
Browser may ask for permission about download multiple files to end user. Simple example for some static files:
<script>
{% set pdfOptions = {
date: entry.dateUpdated|date('U')
} %}
var files = ["{{ alias('@web') }}/{{craft.documentHelper.pdf('_pdf/document.twig', 'file', 'pdf/' ~ entry.dateCreated|date("Y-m-d") ~ random(10) ~ '.pdf' ,entry, pdfOptions)}}", "{{ alias('@web') }}/{{craft.documentHelper.pdf('_pdf/document.twig', 'file', 'pdf/' ~ entry.dateCreated|date("Y-m-d") ~ random(10) ~ '.pdf' , entry, pdfOptions)}}"];
for (var i = files.length - 1; i >= 0; i--) {
var a = document.createElement("a");
a.target = "_blank";
a.download = "download";
a.href = files[i];
a.click();
};
</script>
Simple example with loop:
{% set pdfOptions = {
date: entry.dateUpdated|date('U')
} %}
<script>
var files = [
{% for item in craft.entries.section('xxx').orderBy('title asc').all() %}
"{{alias('@web')}}/
{{craft.documentHelper.pdf("_pdf/document.twig", "file", 'pdf/' ~ item.id ~ '.pdf', item, pdfOptions)}}"
{% if loop.last %}{% else %}, {% endif %}
{% endfor %}
];
for (var i = files.length - 1; i >= 0; i--) {
var a = document.createElement("a");
a.target = "_blank";
a.download = "download";
a.href = files[i];
a.click();
};
</script>
Hey there
Is it possible to download multiple files in a for loop? Something like this:
Thank you for your answer.