<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>
我已经尝试过此代码,但此代码未创建文本文件。提交后它只显示下载选项,但我想在任何驱动器中存储文本文件
答案 0 :(得分:1)
在Javascript中无法通过浏览器将文件保存到主机PC上的任何位置,因为这会给客户端带来很大的安全风险。您可以要求他们下载文件或使用window.localStorage