5

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

nishan
  • 51
  • 1
  • 4
  • Presumably that screenshot is from some sort of _download_ screen. How are you downloading the file? – Phil Feb 26 '19 at 04:46
  • The screenshot is of my browser screen where I am showing the pdf file. – nishan Feb 26 '19 at 05:11
  • Could you perhaps take a larger screenshot. It's unclear where the filename is being applied. What actually happens in order to get to the image in your question? – Phil Feb 26 '19 at 05:13
  • Ah never mind, I see now. It's how Chrome renders the `` tag. – Phil Feb 26 '19 at 05:17
  • I found a duplicate but I don't think you're going to like it, sorry :( – Phil Feb 26 '19 at 05:20
  • Added a larger image. Perhaps you check. – nishan Feb 26 '19 at 05:22
  • Is there any way to do this, i am stucking with the same and not found any solution even in the questions which showing duplicate. – harpal Jul 15 '20 at 06:01

1 Answers1

1

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();
}
wentjun
  • 40,384
  • 10
  • 95
  • 107