Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function saveTextAsFile()
- {
- var textToWrite = //Your text input;
- var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
- var fileNameToSaveAs = //Your filename;
- var downloadLink = document.createElement("a");
- downloadLink.download = fileNameToSaveAs;
- downloadLink.innerHTML = "Download File";
- if (window.webkitURL != null)
- {
- // Chrome allows the link to be clicked
- // without actually adding it to the DOM.
- downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
- }
- else
- {
- // Firefox requires the link to be added to the DOM
- // before it can be clicked.
- downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
- downloadLink.onclick = destroyClickedElement;
- downloadLink.style.display = "none";
- document.body.appendChild(downloadLink);
- }
- downloadLink.click();
- }
- My text area:<br />
- <textarea rows='10' cols='80' id='myTextArea' ></textarea>
- <br /><br />
- Download button: <br />
- <input value='download' type='button'
- onclick='doDL(document.getElementById("myTextArea").value)' />
- <script type='text/javascript'>
- function doDL(s){
- function dataUrl(data) {return "data:x-application/text," + escape(data);}
- window.open(dataUrl(s));
- }
- </script>
- function doDownload(str) {
- function dataUrl(data) {
- return "data:x-application/xml;charset=utf-8," + escape(data);
- }
- var downloadLink = document.createElement("a");
- downloadLink.href = dataUrl(str);
- downloadLink.download = "foo.xml";
- document.body.appendChild(downloadLink);
- downloadLink.click();
- document.body.removeChild(downloadLink);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement