3

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.

4
  • Where do you want to save this file? If on the server, you will need to write some server side code. 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 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. 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. Commented Jan 20, 2014 at 9:32

2 Answers 2

3

I 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 recommend hosting the files, not my project I don't know your use cases.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side

http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

http://eligrey.com/demos/FileSaver.js/

Sign up to request clarification or add additional context in comments.

Comments

0

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....

1 Comment

No, sorry. This is to send or receive from a backend server. I want to run a "save file" from javascript comand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.