0

I am trying to open blob pdf in new tab using Angular 8. The pdf file is opening with default GUID which is as follow:

blob:http://localhost:4200/3eab97c3-ee5e-4980-80b2-99dbb635eae0

But I want to set custom file name once user will try to download that file. My code is as follows:

openInNewWindow(blob: Blob,fileName: string = this.fileName,type: string = this.type) {
    const mimetype = type || blob.type;
    try {
        fileName = this.cruxTranslationService.translate(fileName);
    } catch (ex) {}

    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        const data = new Blob([blob], { type: mimetype });
        window.navigator.msSaveOrOpenBlob(data, fileName + this.getFileExtension(mimetype));
    } else if (window.navigator.userAgent.match('CriOS')) {
        //ios chrom specific condition
        var reader = new FileReader();
        const newBlob = new Blob([blob], { type: mimetype });
        var url = window.URL.createObjectURL(newBlob);
        reader.onloadend = function() {
        window.open(url);
        };
        reader.readAsDataURL(newBlob);
        setTimeout(() => {
        this.newWindow.document.title = 'Application202102We120000.pdf';
        }, 1000);
    } else {
        const file = new File([blob], fileName, { type: mimetype });
        const downloadURL = window.URL.createObjectURL(file);
        if (navigator.userAgent.indexOf('Chrome') !== -1) {
        this.newWindow = window.open(downloadURL, fileName);
        } else if (
        navigator.userAgent.indexOf('Safari') !== -1 &&
        navigator.userAgent.indexOf('Chrome') === -1
        ) {
        this.newWindow.location.assign(downloadURL);
        } else {
        this.newWindow = window.open('', fileName);
        const embed = document.createElement('object');
        embed.data = downloadURL;
        embed.type = mimetype;
        embed.width = '100%';
        embed.height = '100%';
        this.newWindow.document.write(embed.outerHTML);
        this.newWindow.document.close();
        }
        setTimeout(() => {
        this.newWindow.document.title = "Application202102We120000.pdf";
        }, 1000);
    }
}

Can anyone assist me regarding this?

Emdad
  • 822
  • 7
  • 14

1 Answers1

1

You cant Once file is opened in new tab pdf browsers default pdf viewr is used so browser has control over it. Alternatively you can name a pdf and download it before viewing.

Rashidtvmr
  • 108
  • 8