Is is possible to download and upload a file in a JavaScript function without a backend server? I need to export and import a XML made by a JavaScript function. I would like to create button "Save xml" to save a file but I don't know if it is possible. By other hand I wish to upload a XML file directly to JavaScript. If it is not possible I will use a backend server just like a proxy.
Is is possible to download and upload a file in a JavaScript function without a backend server? I need to export and import a XML made by a JavaScript function. I would like to create button "Save xml" to save a file but I don't know if it is possible. By other hand I wish to upload a XML file directly to JavaScript. If it is not possible I will use a backend server just like a proxy.
Share Improve this question edited Jan 20, 2014 at 9:54 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Jan 20, 2014 at 9:11 TobiaTobia 9,57929 gold badges120 silver badges242 bronze badges 4- Where do you want to save this file? If on the server, you will need to write some server side code. – Abijeet Patro Commented Jan 20, 2014 at 9:14
- To the client, from browser to client machine as the usual file download (save file as...) or upload – Tobia Commented Jan 20, 2014 at 9:16
- Is the XML something that the user has created/edited on the web page using JavaScript, and then you want them to download their created file? (so all client based) Or is it a pre-made XML file that you just want them to download? If it's the latter, then the XML will need to be sitting on the same server as your web page is hosted. – seanholden Commented Jan 20, 2014 at 9:21
- It is a java-script made xml, all client based. I wrote "xml" but can be a java-script generated string. – Tobia Commented Jan 20, 2014 at 9:32
2 Answers
Reset to default 3I think you could look in to the HTML5 file api. Based on what you said all file should never leave the domain of the browser or the webapp. I believe this could cause some problems the user would be forced to use the same browser to access files. Also HTML5 standards have still not been fully implemented by all major browsers, that would be another risk. However if you want to go down that route I provided some resources that may help. I would remend hosting the files, not my project I don't know your use cases.
http://www.html5rocks./en/tutorials/file/dndfiles/
http://updates.html5rocks./2011/08/Saving-generated-files-on-the-client-side
http://updates.html5rocks./2011/08/Downloading-resources-in-HTML5-a-download
http://eligrey./demos/FileSaver.js/
I am not sure If I understand this well but it seems like you want to submit data to a remote client and also receive data.
Receiving and saving XML Since its XML you can use an ajax post/get* and parse your xml data from the receiving end and you will want to have an object that holds your XML data in case you want to save.
var myXMLContainer = "";
$.ajax({
type: "Get",
datatype: "xml",
url: "your url sending xml data",
data: xmlData,
success: function (result) {
myXMLContainer = $.parseXML(result);
$("#myHiddenDiv").text(myXMLContainer);
}
});
Saving your data. Since JavaScript doesn't have access to writing files as this would be a huge security risk to say the least. You would set your data to a hidden div and set it to btoa to save your data.
function saveData(){
btoa($("#myHiddenDiv").text());
}
I hope this is clear enough to get by....