简体   繁体   中英

Download Multiple files in one HTTP request

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.

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.

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.

我认为这是不可能的,因为每个 HTTP 请求只有一个 URI。

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.

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 .

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 (eg 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.

One easy way is to encode the files in base64 and send in a JSON. Though, that would make the response something like 4/3 the size, so definitely don't use that on large files.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM