Javascript 提示用户将输出下载为txt文件
我正在寻找帮助,该脚本允许用户通过单击我的“导出”按钮从文本区域下载输出(提示他们保存)Javascript 提示用户将输出下载为txt文件,javascript,Javascript,我正在寻找帮助,该脚本允许用户通过单击我的“导出”按钮从文本区域下载输出(提示他们保存) 非常感谢任何帮助。使用您的方法,只需将textarea的值传递到createObjectURL函数中即可 <textarea id="output" class="form-control text-box noresize" rows="4" placeholder="This text will be downloaded as a file."> </textarea> &l
非常感谢任何帮助。使用您的方法,只需将textarea的值传递到createObjectURL函数中即可
<textarea id="output" class="form-control text-box noresize" rows="4" placeholder="This text will be downloaded as a file.">
</textarea>
<br>
<button id="download">Download</button>
<script>
document.getElementById('download').addEventListener("click", download);
function download(){
var text = document.getElementById('output');
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([text.value], {type: 'text/plain'}));
a.download = 'test.txt';
document.body.appendChild(a)
a.click();
document.body.removeChild(a)
}
</script>
下载
document.getElementById(“下载”).addEventListener(“单击”,下载);
函数下载(){
var text=document.getElementById('output');
var a=window.document.createElement('a');
a、 href=window.URL.createObjectURL(新Blob([text.value],{type:'text/plain'}));
a、 下载='test.txt';
document.body.appendChild(a)
a、 单击();
document.body.removeChild(a)
}
试试这个:
var txt=document.getElementById('content').value;
document.getElementById('link')。onclick=function(){
this.href='data:text/plain;charset=utf-8',+encodeURIComponent(txt);
};
document.getElementById('content')。onchange=function(){
txt=document.getElementById('content')。值;
};代码>
你好,世界
查看这篇博文会让你走上正轨。这篇博文可能在Chrome中重复。有关函数createObjectURL的更多详细信息,请参见此处:
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(['Test,Text'], {type: 'text/csv'}));
a.download = 'test.csv';
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
<textarea id="output" class="form-control text-box noresize" rows="4" placeholder="This text will be downloaded as a file.">
</textarea>
<br>
<button id="download">Download</button>
<script>
document.getElementById('download').addEventListener("click", download);
function download(){
var text = document.getElementById('output');
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([text.value], {type: 'text/plain'}));
a.download = 'test.txt';
document.body.appendChild(a)
a.click();
document.body.removeChild(a)
}
</script>