admin管理员组文章数量:1022719
I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code
const downloadHandler = (file) => {
const a = document.createElement("a");
a.href = file;
a.setAttribute(`download`, file);
a.click();
};
I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code
const downloadHandler = (file) => {
const a = document.createElement("a");
a.href = file;
a.setAttribute(`download`, file);
a.click();
};
Share
Improve this question
edited Aug 30, 2022 at 11:10
Saad Nasim Ullah
asked Aug 30, 2022 at 10:57
Saad Nasim UllahSaad Nasim Ullah
411 gold badge1 silver badge5 bronze badges
2
-
Set
target
to_blank
on the link. If it's JavaScript-initiated, usewindow.open
. – CherryDT Commented Aug 30, 2022 at 11:01 - See this question, I think you can do it without opening a new page. stackoverflow./questions/11620698/… – UnnamedXAer Commented Aug 30, 2022 at 11:02
2 Answers
Reset to default 1<a href="https://yourlink." target="_blank" download>fileName</a>
This will open the HTML file in a new tab
I remend using the download attribute for download instead of jQuery:
<a href="your_link" download> file_name </a>
This will download your file, without opening it.
I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code
const downloadHandler = (file) => {
const a = document.createElement("a");
a.href = file;
a.setAttribute(`download`, file);
a.click();
};
I am trying to download file by clicking on download button it should open the new tab and start downloading and once the download starts it closes the tab itself basically I do not want to have any effect on the original page. Here is my current code
const downloadHandler = (file) => {
const a = document.createElement("a");
a.href = file;
a.setAttribute(`download`, file);
a.click();
};
Share
Improve this question
edited Aug 30, 2022 at 11:10
Saad Nasim Ullah
asked Aug 30, 2022 at 10:57
Saad Nasim UllahSaad Nasim Ullah
411 gold badge1 silver badge5 bronze badges
2
-
Set
target
to_blank
on the link. If it's JavaScript-initiated, usewindow.open
. – CherryDT Commented Aug 30, 2022 at 11:01 - See this question, I think you can do it without opening a new page. stackoverflow./questions/11620698/… – UnnamedXAer Commented Aug 30, 2022 at 11:02
2 Answers
Reset to default 1<a href="https://yourlink." target="_blank" download>fileName</a>
This will open the HTML file in a new tab
I remend using the download attribute for download instead of jQuery:
<a href="your_link" download> file_name </a>
This will download your file, without opening it.
本文标签: reactjsfile download in new tab in javascriptStack Overflow
版权声明:本文标题:reactjs - file download in new tab in javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509134a2153746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论