简体   繁体   English

文件(pdf、jpg、png)下载而不是在新标签中打开

[英]File (pdf, jpg, png) download instead of opening in new tab

I would like to download document instead opening it in new tab.我想下载文档而不是在新标签中打开它。 Files with the extension.docx download normally, but.pdf or.jpg only open in a new tab.扩展名为.docx的文件下载正常,但.pdf或.jpg只能在新标签页中打开。 I would like pdf and jpg files to be automatically downloaded after clicking like.docx.我希望点击like.docx后自动下载pdf和jpg文件。

Html (cannot be changed): Html(无法更改):

<div *ngFor="let action of documentActions">
   <button (click)="action.makeAction(element)">
       <mat-icon>{{action.name}}</mat-icon>
    </button>
</div>

url is eq: https://web-application.com/somefile.pdf url 是 eq: https://web-application.com/somefile.pdf

I have tired with FileSaver (npm intall file-saver) but still not getting downloaded only new tab open.我已经厌倦了 FileSaver(npm intall 文件保护程序),但仍然没有下载,只有打开新标签。

ts component: ts 组件:

declare var require: any
const FileSaver = require('file-saver');
(...)
        name: 'download',
        makeAction: (elem: DocumentListItem) =>
        {
          const url = elem.documentUrl;
          const filename = elem.name;
          FileSaver.saveAs(url,filename);
        }

i've tired also:我也累了:

 makeAction: (elem: DocumentListItem) =>
        {
          window.open( elem.documentUrl);
        }

or

   makeAction: (elem: DocumentListItem) =>
        {
          window.location.href =  elem.documentUrl;
        }

but with the same effect.但效果相同。 Downloading docx files works in any case.在任何情况下都可以下载 docx 文件。 I will be grateful for help in solving the problem.我将不胜感激帮助解决问题。

Greetings !问候 !

If you could construct document URLs in render phase, you could use simple HTML link with download attribute, like:如果您可以在渲染阶段构建文档 URL,则可以使用带有download属性的简单 HTML 链接,例如:

<a href="[URL]" download="[optional suggested file name]">...</a>

See MDN A element download attribute docs.请参阅MDN A 元素下载属性文档。

To trigger download from button click handler, it should be possible to mimic plain link element with download attribute click.要从按钮单击处理程序触发下载,应该可以使用下载属性单击模拟普通链接元素。 href attribute of that link must either be relative in same origin as document or blob: or data: uri.该链接的href属性必须与 document 或blob:data: uri 同源。

POC working in top level page (tested in current Chrome and Firefox), not working here in SO sandbox iframe: POC 在顶级页面中工作(在当前的 Chrome 和 Firefox 中测试),在 SO 沙箱 iframe 中不工作:

 function dl(url, filename){ var a = document.createElement('a'); a.setAttribute('href',url); a.setAttribute('download', filename||''); document.body.appendChild(a); a.onclick = function(){setTimeout(function(){document.body.removeChild(a)},100)} a.click(); }
 <p>(Not working in SO sandbox iframe.) <p><button onclick=" dl('data:text/plain,test', 'button-download-test.txt') ">download text datauri</button> <p><button onclick=" dl(document.location.href, 'button-download-test.html') ">download this HTML file</button> <p><button onclick=" dl('./probe.png', 'button-download-test.png') ">download png file of same origin</button>

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

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