52

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

zakovyrya
  • 9,203
  • 5
  • 36
  • 28
  • Is that the actual response? Or did you cut out things like Content-length for each attachment? If that doesn't work, I'd more or less assume that it Can't Be Done with the current generation of web browsers. – Joe Jun 25 '09 at 00:14
  • 1
    what is the purpose of this? there may be a better way... – Jonathan Fingland Jun 25 '09 at 00:15
  • Actually, content-length might not even be required now that I'm looking at the rfcs. – Joe Jun 25 '09 at 00:17
  • I found an article that appears to solve the same problem. Haven't tried it yet: [http://www.motobit.com/tips/detpg_multiple-files-one-request/](http://www.motobit.com/tips/detpg_multiple-files-one-request/) – Martin Stein Mar 02 '12 at 16:47
  • @Martin, he is talking about downloading, not uploading. The link you post is about uploading. Cheers. – Nizzy Sep 19 '12 at 17:09

3 Answers3

42

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.

Andrew Moore
  • 87,539
  • 30
  • 158
  • 173
  • 1
    Zipping is not an option because the requirement is to not ask user to perform additional operation after download. Your statement that multipart messages are not intended for HTTP transfers makes no sense. For instance, multiple files are uploaded using multipart/form-data. I don't see any reason why multipart messages should be forbidden for binary downloads – zakovyrya Jun 25 '09 at 00:31
  • 3
    @zakovyrya: Let me rephrase that last statement. multipart messages were never intended to be received and parsed on the client side of an HTTP transaction. – Andrew Moore Jun 25 '09 at 00:34
  • 8
    @Einstein: I miss-phrased my original statement as I assumed people knew the difference between multipart/mixed and multipart/form-data. Again, multipart/* was never intended to be received and parsed on the client side of an HTTP transaction. It is created on the client side during file upload and is received and parsed server side. – Andrew Moore Jun 25 '09 at 00:41
  • Sorry, your comment appeared only after I sent mine. Maybe its not some W3C/IETF sanctioned thing but I do remember doing this myself over a decade ago. At the time it was part of "server side push" – Einstein Jun 25 '09 at 00:59
  • I agree, the only way to do this is to dynamically create a zip file and send to the user. – Roy Rico Jun 25 '09 at 01:40
  • I'll try to download multiple files using javascript in the hidden iframe. Will keep you posted – zakovyrya Jun 25 '09 at 04:38
  • For those wondering about zip: there is Python's [zipfile library](https://docs.python.org/3/library/zipfile.html) and e.g. [Serving dynamically generated ZIP archives in Django](https://stackoverflow.com/q/67454) – djvg Oct 29 '20 at 13:28
1

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.

Einstein
  • 4,290
  • 1
  • 21
  • 19
0

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.

Roy Rico
  • 3,475
  • 4
  • 31
  • 36
  • 1
    Data URIs are pretty well supported now (http://stackoverflow.com/questions/1765342/which-browsers-support-data-uris-and-since-which-version), but they won't help with this question. – Brilliand Jul 18 '13 at 17:24