How to download multiple files with one HTTP request?

HttpBrowserMultipart

Http Problem Overview


Use case: user clicks the link on a webpage - boom! load of files sitting in his folder.
I tried to pack files using multipart/mixed message, but it seems to work only for Firefox

This is how my response looks like:

HTTP/1.0 200 OK
Connection: close
Date: Wed, 24 Jun 2009 23:41:40 GMT
Content-Type: multipart/mixed;boundary=AMZ90RFX875LKMFasdf09DDFF3
Client-Date: Wed, 24 Jun 2009 23:41:40 GMT
Client-Peer: 127.0.0.1:3000
Client-Response-Num: 1
MIME-Version: 1.0
Status: 200

--AMZ90RFX875LKMFasdf09DDFF3 
Content-type: image/jpeg 
Content-transfer-encoding: binary 
Content-disposition: attachment; filename="001.jpg" 

<< here goes binary data >>--AMZ90RFX875LKMFasdf09DDFF3 
Content-type: image/jpeg 
Content-transfer-encoding: binary 
Content-disposition: attachment; filename="002.jpg" 

<< here goes binary data >>--AMZ90RFX875LKMFasdf09DDFF3 
--AMZ90RFX875LKMFasdf09DDFF3--

Thank you

P.S. No, zipping files is not an option

Http Solutions


Solution 1 - Http

Zipping is the only option that will have consistent result on all browsers. If it's not an option because you don't know zips can be generated dynamically, well, they can. If it's not an option because you have a grudge against zip files, well..

MIME/multipart is for email messages and/or POST transmission to the HTTP server. It was never intended to be received and parsed on the client side of a HTTP transaction. Some browsers do implement it, some others don't.

As another alternative, you could have a JavaScript script opening windows downloading the individual files. Or a Java Applet (requires Java Runtimes on the machines, if it's an enterprise application, that shouldn't be a problem [as the NetAdmin can deploy it on the workstations]) that would download the files in a directory of the user's choice.

Solution 2 - Http

Remember doing this >10 years ago in the netscape 4 days. It used boundaries like what your doing and didn't work at all with other browsers at that time.

While it does not answer your question HTTP 1.1 supports request pipelining so that at least the same TCP connection can be reused to download multiple images.

Solution 3 - Http

You can use base64 encoding to embed an (very small) image into a HTML document, however from a browser/server standpoint, you're technically still sending only 1 document. Maybe this is what you intend to do?

Embedd Images into HTML using Base64

EDIT: i just realized that most methods i found in my google search only support firefox, and not iE.

Solution 4 - Http

You could make a json with multiple data urls.

Eg:

{
    "stamp.png": "data:image/png;base64,...",
    "document.pdf": "data:application/pdf;base64,..."
}

Solution 5 - Http

(extending trinalbadger587's answer)

You could return an html with multiple clickable, downloadable, inplace data links:

<html>
<body>
  <a download="yourCoolFilename.png" href="data:image/png;base64,...">PNG</a>
  <a download="theFileGetsSavedWithThisName.pdf" href="data:application/pdf;base64,...">PDF</a>
</body>
</html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzakovyryaView Question on Stackoverflow
Solution 1 - HttpAndrew MooreView Answer on Stackoverflow
Solution 2 - HttpEinsteinView Answer on Stackoverflow
Solution 3 - HttpRoy RicoView Answer on Stackoverflow
Solution 4 - Httptrinalbadger587View Answer on Stackoverflow
Solution 5 - HttpIvan AkcheurovView Answer on Stackoverflow