OK, so my question today is a bit vague, but it's one that a lot of people seem to be asking. Is there anyway to save a file from an HTML page that isn't strictly IE specific? I am trying to write an HTML page that opens and XML file and lets you edit the file in the page and save it. Obviously as a general practice this is bad, since there are huge security implications with writing files using scripts, but there must be some way. I am using JavaScript for most of the manipulation of the data, but after I make changes to the XML file, I want to be able to see these changes reflected in the saved copy on my hard disk. What options are there for something like this?
-
No, there isn't such way. Saving files on the client computer using javascript would have been a horrible security vulnerability and thanks God this is not possible. When you mention IE I guess you mean some ActiveX solutions. – Darin Dimitrov Sep 10 '13 at 20:44
-
You can only make a browser offer you a download, you can't choose where the file is written. Basic concept is in a previous answer of mine [**here**](http://stackoverflow.com/a/14680193/1615483) but you'd most likely want to to generate a _[**ObjectURL**](https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL)_ from a _Blob_ which is the data you want saved. – Paul S. Sep 10 '13 at 20:44
-
Send it to the server with ajax, store it there for a bit while the client grabs it with `window.location = '/grabfile.php?id='+id;` Serve the file from grabfile.php with `header('Content-disposition: attachment; filename='.$filename);` And then the browser will download==save the file. Awesome. – DDS Sep 10 '13 at 20:45
-
it's very possible now, has been for about a year... use an input tag or dnd to open the file from the user's computer. use a download function to save the modified data back to the computer. http://danml.com/js/download.js works like download(strMyFileData, "data.txt", "text/plain"); – dandavis Sep 10 '13 at 20:49
-
To be clear, I don't want to save the file on the client side, i want to modify the files being hosted on the server side – adam5990 Sep 11 '13 at 18:45
4 Answers
I don't think you can do that, however, you can use php to create a file with whatever content the user specified and save it on your server. At which point, you could then return the link to the user to view. It would work even better if you had a back end database.
Like I said, this option would actually be creating files on your server, not changing them.
Useful Link:
http://www.tech-recipes.com/rx/1455/php-create-a-file-on-your-server/

- 3,682
- 2
- 20
- 43
I'll suggest to check nodejs. I wrote an extension for Chrome, which does the exactly same thing. It opens a file from my hard disk, I'm able to edit it and later save it. I just used nodejs and sockets as a bridge between the browser and the file system. Here is a video which demonstrates the process: http://www.youtube.com/watch?v=exAWuCZFCSc And probably because you already know javascript you will be able to write your own module easily.

- 13,306
- 3
- 40
- 55
You can try this:
var dataXml = '<?xml version="1.0" encoding="ISO-8859-1"?><note><from>Myself</from><to>World</to><message>Hello world!</message></note>';
var url = 'data:text/xml;charset=utf8,' + encodeURIComponent(dataXml);
window.open(url, '_blank');
window.focus();

- 11,861
- 21
- 74
- 119
as long as you don't care about old machines, it's easy using html5's JS API:
live demo: http://danml.com/iotest.html
<input type=file id=file onchange=pop(this)>
<button onclick="download(data.value, file.files[0].name, file.files[0].type )">save</button>
<br />
<textarea cols=110 rows=20 id=data ></textarea>
<script src="http://danml.com/js/download.js" ></script>
<script>
function pop(file){
var fr=new FileReader();
fr.onload=function(e){data.value=e.target.result;}
fr.readAsText(file.files[0]);
}
</script>

- 16,370
- 5
- 40
- 36