84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
Is there a way to create a text file on the client side and prompt the user to download it without any interaction with the server?
I know I can't write directly to their computer (security, etc.), but can I create the file and prompt them to save?
Simple solution for HTML5 browsers...
function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); }
form * { display: block; margin: 10px; }
use
download('test.txt', 'Hello world!');
You can use data URI. Browser support varies; seeWikipedia. Example:
text file
Octet stream is used to force download prompts. Otherwise, it may open in the browser.
For CSV you can use:
CSV Octet
TryjsFiddle Demo.
Simple solution for HTML5 browsers...
use
You can use data URI. Browser support varies; seeWikipedia. Example:
Octet stream is used to force download prompts. Otherwise, it may open in the browser.
For CSV you can use:
TryjsFiddle Demo.