简体   繁体   中英

Downloading a pdf without opening new tab or changing current tab view

I have a pdf I am receiving from an api call and I want to accomplish downloading the file to my computer without it opening a new tab or opening the url in the current tab I have open. I realize this is a question frequently asked but all I can find are examples accomplishing the opening new tab or loading in the current tab.

Ive tried using an tag like:

         <Button
              className="save-sign-note"
            >
              <a href={pdfURL} download={`${props.visitType}`}>
              Download and Next <RightArrow /> 
              </a>
            </Button>

and this downloads the file in the current window.

Also Ive tried using a handler like:

<Button
onClick={() => DowloadAndNext()}
>...

const DowloadAndNext = () => {
     window.location.href = pdfURL;
  };

and

<Button
onClick={() => DowloadAndNext()}
>...

const DowloadAndNext = () => {
    const url = pdfURL;
    const link = document.createElement("a");
    link.href = url;
    link.setAttribute("download", "file.pdf");
    document.body.appendChild(link);
    link.click();
  };

but I get the same result each time. Any help with this would be awesome as I am new to developing front end.

You can't control this from your website for pdf downloads. This is a browser settings behavior when downloading PDFs. Changing the default pdf software to Adobe reader instead of edge may solve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM