下载PDF而不是在浏览器中打开。

huangapple go评论59阅读模式
英文:

Download pdf instead of opening in browser

问题

我正在尝试在我的 JavaScript 中下载一个 tiff 和 pdf 文件,但这只适用于 tiff 文件。pdf 文件会在浏览器中打开,改变我的用户界面的视图,这是不希望的。由于我只能在该环境中使用 IE 9,所以我被困在这里。

<a href="<%=fileName%>" download="<%=fileName%>"><%=fileName%</a>
英文:

I am trying to download a tiff and pdf in my javascript but this only works for tiff files. The pdf ones open in the browser changing the view of my UI which is not desired. I am stuck with using IE 9 as that is only browser I can use in that environment.

&lt;a href=&quot;&lt;%=fileName%&gt;&quot; download=&quot;&lt;%=fileName%&gt;&quot;&gt;&lt;%=fileName%&gt;&lt;/a&gt;

答案1

得分: 1

只需将 target=&quot;_blank&quot; 属性添加到锚 &lt;a&gt; 元素中:

&lt;a href=&quot;&lt;%=fileName%&gt;&quot; download=&quot;&lt;%=fileName%&gt;&quot; target=&quot;_blank&quot;&gt;&lt;%=fileName%&gt;&lt;/a&gt;

这将在新标签页/窗口中打开 PDF(如果这是默认浏览器行为)。您不会离开您的网站。如果不会自动下载,您现在可以从标签页中保存 PDF。

英文:

Simply add the target=&quot;_blank&quot; attribute to the anchor &lt;a&gt; element:

&lt;a href=&quot;&lt;%=fileName%&gt;&quot; download=&quot;&lt;%=fileName%&gt;&quot; target=&quot;_blank&quot;&gt;&lt;%=fileName%&gt;&lt;/a&gt;

This will open the PDF in a new tab/window (if that is the default browser behavior). You will not navigate away from your website. You can now save the PDF from the tab, if it doesn't automatically get downloaded.

huangapple
  • 本文由 发表于 2023年3月4日 05:09:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定