10

Good day.

I'm new to jQuery, and have a passing familiarity with javascript, having spent most of my time on the server side.

My interest is in posting in the browser a multipart/form-data form object consisting of one text field and one file. In response, the server returns a multipart/mixed response consisting of one part html or json, and one part application/octet-stream.

My goal is to learn how to extract with jQuery the html or json part and optionally display it in a target div (if html) or redirect to a URL in the json (if json), and save the octet-stream to disk. Preferably in that order.

Would someone be kind enough to comment on whether such multipart/mixed response parsing is possible with jQuery and some idea of how to do this.

Actually, while I much prefer jQuery, I'll learn something from answers framed in any popular javascript framework, and even unadorned javascript itself.

Thank you.

ae6rt
  • 2,668
  • 3
  • 24
  • 25
  • See the answers to the same, but framework-neutral question in http://stackoverflow.com/questions/12066640/parsing-a-formdata-object-with-javascript – Wolfgang Kuehn Feb 07 '14 at 17:52

2 Answers2

2

GREAT question. Judging by the existence of this plugin im assuming no:

I also found this link in a discussion of that plugin, which might be helpful to you:

http://about.digg.com/blog/duistream-and-mxhr

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 2
    Thanks. Yes, I read about this plugin recently. What I could not discern was whether this plugin could be used if I include an entry for 'application/octet-stream' or not. I did a fair amount of googling around for this subject, and it seems not a lot of people are talking about parsing multipart responses. I can understand why, but would have expected more discussion (and use cases) than I actually found. – ae6rt Sep 11 '10 at 14:50
  • Yeah... ive been doing php/js/html/css development for YEARS and id never even thought of a multi-part response until I saw a few questions on it here on SO over the past week or two. Interesting stuff though. – prodigitalson Sep 11 '10 at 15:01
  • @ae6rt, assuming you manage to successfully parse the `multipart/mixed` response, what do you intend to do with the `application/octet-stream` part? – Darin Dimitrov Sep 11 '10 at 15:11
  • The multipart/mixed response consists of a slug of html that describes (metadata) the result of the form post. The octet-stream part is actually another file that is the result (data) of the form post. – ae6rt Sep 11 '10 at 15:14
  • OK, but what is the application going to do with the file stream? I am asking this because in your question you mentioned something about saving it on disk. What disk? – Darin Dimitrov Sep 11 '10 at 15:18
  • Sounds like I'm missing something :-) After I have the octet-stream bytes in-hand, my plan was to put up a dialog in the browser asking the user where (to what filename) to save 'the file'. – ae6rt Sep 11 '10 at 15:26
  • Oh, that's just not going to work with AJAX. The Save dialog is invoked only when a Content-Disposition header is sent from the server and the request was not made using AJAX. – Darin Dimitrov Sep 11 '10 at 15:29
  • I see. It is not out of the question to return *only* json (the use case is now no longer one where multipart/mixed is returned, but instead application/json) with a URL to where the resulting file can be retreived from the server with HTTP GET. In which case the treatment of the response just got a whole lot simpler. – ae6rt Sep 11 '10 at 15:37
-1

Have a look at the jQuery's API. If your AJAX call returns HTML you can use the .load function. If it returns JSON you can use the .getJSON function.

jerone
  • 16,206
  • 4
  • 39
  • 57
  • Thanks. In fact, the server will always return multipart/mixed. For the sake of discussion, we can assume the parts will always be 'text/html' and 'application/octet-stream'. – ae6rt Sep 11 '10 at 14:53