views:

112

answers:

2

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.

A: 

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
not really what hes asking
prodigitalson
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
A: 

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

http://plugins.jquery.com/project/mpAjax

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

prodigitalson
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
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
@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
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
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
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
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
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
Marking the thread with the most information content.
ae6rt