Download Multiple files in one HTTP request

2019-01-12 05:03发布

问题:

how is it possible to download multiple files in one HTTP request? what i mean it's like when you have multiple attachments and you select what you want to download then press download so they will be automaticcaly downloaded and you don't have to click on each one manually.

i'm using PHP as a serverside srcipting.

回答1:

It is possible to send a multipart response in HTTP:

In general, HTTP treats a multipart message-body no differently than any other media type: strictly as payload. […] an HTTP user agent SHOULD follow the same or similar behavior as a MIME user agent would upon receipt of a multipart type.

[…] If an application receives an unrecognized multipart subtype, the application MUST treat it as being equivalent to "multipart/mixed".

But since Firefox is the only browser that I know about to support such multipart responses (apart from multipart/byterange), you should use some archive file format for this purpose.



回答2:

That is practically not usable due to poor browser support. You can pack them into a tar or zip file at server side and serve the archive file though.



回答3:

I think it's not possible since each HTTP request has only one URI.



回答4:

You can zip the file with PHP, serverside, and request the file or return it from within your script by setting the appropriate headers, see ZipArchive class

Or you create a special client that can parse your then self-specified message format (a flash app, a plugin) - but if your client is simply your browser you'll get one response with a fixed content-length from the server.



回答5:

Dunno about one HTTP request, but if you want them all at once you can loop through them while changing the header('Location:') for each of them while redirecting to the immediate download script. Though, that would be redundant, and ugly; I think the best way would be to zip them all, there are instructions on how to do so in the PHP Documentation.



回答6:

I'm using multiple invisible iframes to trigger multiple downloads at once. There will be more than one HTTP request, but the effect will be what you described.

<iframe style="display: none;" src="file1.zip"></iframe>
<iframe style="display: none;" src="file2.zip"></iframe>
<iframe style="display: none;" src="file3.zip"></iframe>

Most browsers will ask the user if they want to allow multiple downloads (this is to prevent letting website spamming the file system). Also, you have to make sure the file will actually be downloaded, and not just displayed inside the invisible iframe (e.g. by using header('Content-disposition: attachment'); inside the PHP script serving the file).

In my solution I use JavaScript to change the src after each file is downloaded, so the files load sequentially and my server has to handle fewer requests at once. But that solution requires AJAX and is much more complicated.



回答7:

May be you can use JS to open multiple popups each loading a download URL. I hope its the only way..