Not able to rename pdf file while downloading in Javascript

Viewed 70

I am trying to download the pdf file from the server using javascript. This is my url. https://domainurl.amazonaws.com/f6c3b-72c3-4a5-b489-c798e64c4d.pdf?....

I tried in two ways.

  1. The current name of the file is fetching from url (f6c3b-72c3-4a5-b489-c798e64c4d.pdf). The below method is not overriding the existing name because of cross origin request.

     var link = document.createElement("a");
     link.href = uril;
     link.download = `test.pdf`;
     document.body.appendChild(link);
     link.click();
     document.body.removeChild(link);```
    
    
  2. In the second method, i am trying to get that pdf using axios request. server is blocking this because of cors issue.

         axios({
         url: 'http://api.dev/file-download', //your url
         method: 'GET',
         responseType: 'blob', // important
     }).then((response) => {
         const url = window.URL.createObjectURL(new Blob([response.data]));
         const link = document.createElement('a');
         link.href = url;
         link.setAttribute('download', 'file.pdf'); //or any other extension
         document.body.appendChild(link);
         link.click();
     }); ```
    
    

is there any other way to download pdf file using javascript?

0 Answers
Related