adrianstern has asked for the wisdom of the Perl Monks concerning the following question:

I'm not even sure this is a Perl question really but this is my problem

I have a little program in Perl for downloading high resolution photographs from my site as the ones displayed are low resolution for fast loading - and on the web you really don't need high resolution

Currently you can download one photo at a time and it works fine.

I was asked if I couldn't make it possible to download more than one at a time so I had a go. But no matter what I do the browser will only download the first one. all the code runs but there is only one download

This is the relevant code I execute many times

print STDOUT qq(Content-type: application/force-download\n); print STDOUT qq(Content-Disposition:attachment;filename=$photo\n\n); print @fileLines;

Has anyone any idea why this should happen?

Replies are listed 'Best First'.
Re: download multiple photos
by Fletch (Bishop) on Apr 04, 2021 at 17:22 UTC

    It's not a perl question; that being said this SO thread has examples of how you can do this but it's going to require some client side javascript for their solutions. If you want to avoid the JS then you'd need to have your script create an archive (e.g. Archive::Zip) of the multiple files and then send that to the user.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: download multiple photos
by perlfan (Parson) on Apr 05, 2021 at 15:38 UTC
    Options come down to,
    • the server can bundle many images and send them as a single response to a single click
    • the browser can simulate multiple requests using a single click
    • there is a single click that downloads a single file, that is a bundle (zip file, as mentioned below)
    Downloadling multiple files will cause the browser to ask where to place the file if they have it set to do that; if there is a file name conflict, it may or may not get resolved automagically. There are also plugin for bulk downloading, though that doesn't solve the problem that you're displaying the low resolution but want to allow people to download the high res - unless they are smart enough to look at the hyperlinks (e.g., look for ahrefs that end in .jpg), and if that's possible then just having the links somewhere in the HTML source might be sufficient (maybe hyperlink the low res images to them in the main view?).

    For you, the idea of selecting which to download using checkboxes, then having the server serve them a custom zip seems to be the most user friendly. But the implementation of this for you is the more complicated (not hard though) of all your options. It'd just require a: form (checkboxes) + handler logic to zip the "selected" files and respond back with a file download of the .zip.