Is there a way to automatically download PDF and JPG files instead of opening them in a new tab like .docx files? Currently, when clicking on a PDF or JPG link, it opens in a new tab rather than downloading the file. The desired behavior is for these files to be downloaded upon clicking.
HTML (cannot be changed):
<div *ngFor="let action of documentActions">
<button (click)="action.makeAction(element)">
<mat-icon>{{action.name}}</mat-icon>
</button>
</div>
URL example:
I have tried using FileSaver (NPM install file-saver) but the files still only open in a new tab rather than being downloaded.
Ts component:
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 also attempted: `
makeAction: (elem: DocumentListItem) =>
{
window.open( elem.documentUrl);
}
or
makeAction: (elem: DocumentListItem) =>
{
window.location.href = elem.documentUrl;
}
` but no luck so far. Downloading docx files works without any issues. Any assistance with resolving this problem would be greatly appreciated.