[英]How to trigger a file download with text area content when clicking an HTML button
I Found this on GeeksforGeeks and it solves my some part of issue.我在 GeeksforGeeks 上找到了这个,它解决了我的部分问题。
<script>
function download(file, text) {
//creating an invisible element
var element = document.createElement('a');
element.setAttribute('href',
'data:text/plain;charset=utf-8, '
+ encodeURIComponent(text));
element.setAttribute('download', file);
// Above code is equivalent to
// <a href="path of file" download="file name">
document.body.appendChild(element);
//onClick property
element.click();
document.body.removeChild(element);
}
// Start file download.
document.getElementById("btn")
.addEventListener("click", function() {
// Generate download of hello.txt
// file with some content
var text = document.getElementById("text").value;
var filename = "GFG.txt";
download(filename, text);
}, false);
I tried to make it as per my requirement, i am generating multiple textareas dynamically in a table within and hiding them, so that for each textarea item shows with download icon when some one clicks on that icon it downloads file with textarea with respective each row content.我试图按照我的要求制作它,我在一个表格中动态生成多个 textareas 并隐藏它们,这样对于每个 textarea 项目显示下载图标,当有人点击该图标时,它会下载带有 textarea 的文件和相应的每一行内容。
function download(file, text) { //creating an invisible element var element = document.createElement('a'); element.setAttribute('href', 'data:text/html;charset=utf-8, ' + encodeURIComponent(text)); element.setAttribute('download', file); document.body.appendChild(element); element.click(); document.body.removeChild(element); } document.querySelectorAll('.btn_mop').forEach(item => { item.addEventListener('click', event => { var text = document.querySelector('.text_mop'); var filename = "MOP.txt"; download(filename, text); }, false) })
p { color: green; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <body> <style> </style> <textarea class="text_mop" style="display: none;">Welcome to 1st sentence</textarea> <br/> <input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" /> <textarea class="text_mop" style="display: none;">Welcome to 2nd sentence </textarea> <br/> <input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" /> </body> </html>
It always downloads 1st textarea content even i click on 2nd button, can any please help me how can i get for each download link click it downloads respective textarea content.它总是下载第一个 textarea 内容,即使我单击第二个按钮,请帮助我如何获取每个下载链接,单击它下载相应的 textarea 内容。
SelectorAll
then generated the link without loop through it首先你被SelectorAll
选中了所有 textarea 然后生成了没有循环的链接container
and use this
keyword如果您想在单击按钮之前下载文本区域,最好将它们添加到container
并使用this
关键字pervouisSibling
或使用pervouisSibling
br
tag最好使用 css 而不是br
标签HTML HTML
<div class="container">
<textarea class="text_mop" style="display: none;">Welcome to 1st sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
</div>
<div class="container">
<textarea class="text_mop" style="display: none;">Welcome to 2nd sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
</div>
CSS CSS
.text_mop, .btn_mop {
display: block;
width: auto;
}
JS JS
function download(file, text) {
//creating an invisible element
var element = document.createElement('a');
element.setAttribute('href', 'data:text/html;charset=utf-8, ' + encodeURIComponent(text));
console.log(element.href);
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var btn_mop = document.getElementsByClassName("btn_mop");
for (var btn of btn_mop) {
btn.addEventListener("click", function() {
var text = this.parentNode.getElementsByClassName("text_mop")[0];
var filename = "MOP.txt";
download(filename, text.value)
});
}
HTML HTML
<textarea class="text_mop" style="display: none;">Welcome to 1st sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
<textarea class="text_mop" style="display: none;">Welcome to 2nd sentence</textarea>
<input type="button" class="btn_mop" value="Download" class="fa fa-home" aria-hidden="true" />
CSS CSS
.text_mop, .btn_mop {
display: block;
width: auto;
}
JS JS
function download(file, text) {
//creating an invisible element
var element = document.createElement('a');
element.setAttribute('href', 'data:text/html;charset=utf-8, ' + encodeURIComponent(text));
element.setAttribute('download', file);
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var btn_mop = document.getElementsByClassName("btn_mop");
for (var btn of btn_mop) {
btn.addEventListener("click", function() {
var text = this.previousSibling.previousSibling
var filename = "MOP.txt";
download(filename, text.value)
});
}
A quick solution, though not as well designed as JS_INF's solution is to add the incrementor to the forEach and then use it to identify the correct .text_mop using querySelectorAll()一个快速的解决方案,虽然不像 JS_INF 的解决方案设计得那么好,是将增量器添加到 forEach,然后使用它来识别正确的 .text_mop 使用 querySelectorAll()
document.querySelectorAll(".btn_mop").forEach((item,i) => {
item.addEventListener("click", (event) => {
var text = document.querySelectorAll(".text_mop")[i];
console.log(text)
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.