wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Received xmlhttp result, want to allow user to download - how? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Received xmlhttp result, want to allow user to download - how?" Watch "Received xmlhttp result, want to allow user to download - how?" New topic
Author

Received xmlhttp result, want to allow user to download - how?

andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Using JavaScript to call a servlet via POST and sending along various parameters.
Servlet returns an XML message.
In the same page, XML result is parsed and written into a table.

I want to include a "Download Results" type button or link below the table that will allow the user to download the results (in XML, or a comma delimited file).
My plan was to make another XMLHttp call to another servlet and pass the servlet the same XML response my JS received. Problem is, my JS seems to have "trapped" the XML results - what I'd like is a download prompt (i.e. "Save As...") instead.

Any way to do this?
Or do I need to rewrite the XML results into a hidden form and then submit the form to the servlet?
FWIW, the XML isn't anything too fancy, but I am unfamiliar with how to pass the XML back to the servlet via a form...
andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Semi-update/solution:
If you resend the XML as a parameter in the URL and use an href, it will prompt you (without leaving the page).

i.e.



Servlet (the one called in URL) of course has to have the appropriate response headers set to tell the browser to download the file.

If this is a crappy way of doing it or anyone knows a better way, would love to hear it!
[ October 07, 2008: Message edited by: andrew ennamorato ]
andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
I found a better way (which might be obvious).
The problem with passing via URL params (i.e. GET) is that sometimes my XML response was so long it couldn't be used on a GET call.

Instead, when I receive the XML response, I write a hidden input field into the page. Then, using an HTML button element, I attach a method that simply retrieves that hidden value and then the button makes a POST call. The POST call is to a servlet that just returns the XML but as a file (i.e. the user gets a 'Download File as...' prompt).

There still might be a better way, but that's how I got it working. I can post a code snipped if you're interested, but it's pretty easy to figure out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Received xmlhttp result, want to allow user to download - how?
 
Similar Threads
Problem with dojo.io.bind
Submitting a table in a form
A problem in hiding a button
Passing resultset from Servlet to JSP
Calling Bean Code From JSP