How can I change a base64 application/pdf file name?

5k views Asked by At

The API I am calling returns me a string like 'JVBERi0xL....(more)'

And I am setting the result value to a variable pdfFile like

var file = "data:application/pdf;base64,"+res.data.result;
// here res.data.result = 'JVBERi0xL....(more)'
this.pdfFile = file;

And my Html code is

<object :data="pdfFile" name='test' type="application/pdf"  width="100%" height="800px"></object>

Like this, I can show the pdf on the browser but failed to change the name of the pdf file.

Help: I need to change the name marked with red.

I need to change the name marked with red.

Added a larger image: Larger version of the previous image

1

There are 1 answers

4
wentjun On

The base64 representation of the file only contains the content of the file, but not the file name. But you can manually assign a file name this way:

function download() {
    const source = 'data:application/pdf;base64,'+res.data.result;
    const downloadLink = document.createElement("a");
    const fileName = 'file.pdf';

    downloadLink.href = source;
    downloadLink.download = fileName;
    downloadLink.click();
}