[jQuery] Download a file made from a String
Hi everyone,
I want to allow my Users to download a file containing nothing but a
string generated in the client. I do have a solution but am rather
unhappy with it. In brief:
Mission:
* In my App the User is presented a list of checkboxes to let him
select which content (already stored in an attribute of each checkbox)
he wants to download as a XML file - pure clientside. Done.
* With jQuery my App puts together a string containing XML-markup
according to User's selection - still clientside. Done.
* Now I want the User to be able to download the contents of the
string (with the common "Save as"-dialogue) in a file named
"MyData.xml" - also clientside...
Solution 1 (please see related code below):
* Send the string via Ajax to the backend, where a XML-file called
"MyData.xml" is created on the local drive.
* If this was successful, window.open() is initiated in the client
linking the XML-file.
* User is happy.
Yet I am unhappy with Solution 1 since
* all data is "uselessly" passed back and forth, even though no
contents are changed (just the 'formatting' as a file)...
* the generated file still needs to be unlinked in a further Ajax-call
after 'x' seconds...
* the file is actually displayed in the browser :( But thats solvable
PHP-Offtopic ;)
Thus I discarded further improvement of Solution 1...
Solution 2:
* Somehow the string is being 'streamed' into the file-download
initiated after the User decided to "Save file as" (no rightclick!).
But how? Help! ;) Do you know any tricks or hints to archive Solution
2 () with jQuery or Javascript?
Many thanks in advance and greetings to a great group!
Aharef
<code>
var filename = "data/MyData.xml";
var xmlString = generateXmlString(); // <?xml version="1.0"?