最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - file download in new tab in javascript - Stack Overflow

programmeradmin0浏览0评论

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, use window.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
Add a ment  | 

2 Answers 2

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.

发布评论

评论列表(0)

  1. 暂无评论