File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
andrew ennamorato
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic