0

I receive pdf in base64 format. when i open it using window.open() pdf download,but name of pdf is download.pdf. i want to custom name that pdf file. Thanks in advance.

   fetch(service_url, {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },

  }).then(response => response.blob())
    .then(response => {

      var blob=response
      var reader = new window.FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = function() {
      var base64data = reader.result;

          window.open(base64data);

      }
    })
    .catch(error => {
      console.error(error);
    });
Nagesh Fultambkar
  • 546
  • 1
  • 7
  • 22

1 Answers1

0

I do have created one util in one project hope this helps.

exportToJson = el => {
    if (!isEmpty(this.formData)) {
      const obj = encodeURIComponent(JSON.stringify(this.formData, null, '\t'));
      const data = "text/json; charset=utf-8," + "obj";
      const date = new Date();

      const fileName = "customName.json";
      el.target.setAttribute('href', 'data:' + data);
      el.target.setAttribute('download', fileName);
    }
  };
Shubhanu Sharma
  • 1,957
  • 12
  • 30
  • What i am doing here is taking some json data and downloading as file so that `el` is an dom element(button) what i am passing in args and setting `download` Attribute, in that we can pass file name too. and my scenario was user will click on button to download. if your scenario is auto download then you can create and hidden button and go same way how i am doing then trigger click on that using js. – Shubhanu Sharma Jun 07 '18 at 07:36