This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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...
Joined: Oct 03, 2007
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).
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 ]
Joined: Oct 03, 2007
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.