Reputation: 57
<textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>
<script>
(function () {
var textFile = null,
makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create'),
textbox = document.getElementById('textbox');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();
</script>
I have already tried this code but this code is not creating a text file. After submitting it is just showing download option but I want to store a text file in any drive
Upvotes: 0
Views: 138
Reputation: 317
It is not possible in Javascript to save files through the browser to anywhere on the host PC as that would pose a great security risk to the client. You can either ask them to download the file or usewindow.localStorage
Upvotes: 1