简体   繁体   English

在不打开新标签或更改当前标签视图的情况下下载 pdf

[英]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.我有一个 pdf 我从 api 呼叫接收,我想完成将文件下载到我的计算机而不打开新选项卡或打开 url 的当前选项卡。 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.这将下载当前 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.您无法从您的网站控制 pdf 下载。 This is a browser settings behavior when downloading PDFs.这是下载 PDF 时的浏览器设置行为。 Changing the default pdf software to Adobe reader instead of edge may solve this.将默认的 pdf 软件更改为 Adobe reader 而不是 edge 可能会解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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