0

I am calling a rest api to my server and server is returning me path of stored pdf such as

API Reponse :

"http://xxx.xxx.xxx.xxx/files/mypdf.pdf"

Javascript Code

<script>
function download(filename) {
    var element = document.createElement('a');
    element.setAttribute('href', filename);
    element.setAttribute('download', filename);

    element.style.display = 'none';
    document.body.appendChild(element);

    element.click();

    document.body.removeChild(element);
}

<button onClick="download('http://xxx.xxx.xx.xx/pdf/mypdf.pdf')>Click here to download pdf</button>

</script>
</p>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

When i am clicking on button new tab is opening and PDF is showing but i wants to directly download it on click on button.

How i can directly download PDF on browser from Rest Path. Please help me on this.

Nirmal Kumar
  • 125
  • 8
  • "When i am clicking on button new tab is opening and PDF is showing but i wants to directly download it on click on button."...it's doing that because your browser supports displaying PDFs. _Technically_, the document is downloaded to your device (otherwise you wouldn't be able to see it in your browser) it just gets placed in a temporary folder. You can of course always save it permanently from the browser display using the options provided. If you want it to download directly you'd have to use a browser which doesn't have PDF support, or disable it in your current browser. – ADyson Jun 14 '18 at 09:58
  • It's also possible that if it's your API, you may be able to modify the headers the API sends along with the PDF such that it encourages the browser to offer the file up for download instead of displaying it (I think you have to set the content disposition header to "attachment", from memory - you can easily google exactly what to do), but even then this is merely a hint to the browser, you can't _force_ it to directly download the document if it would rather display it. In other words, there are things you can do, but this is not fully in your control when other users access your API. – ADyson Jun 14 '18 at 09:59
  • Hi Adyson, The problem here is that we are getting only path of pdf in response and response type is application/json and it is an application restriction that we can not change response type on request. Is there have any way around to download file on browser from path ? – Nirmal Kumar Jun 14 '18 at 10:03
  • Yes but you can make an API endpoint which gets the file content and sends it for download, and sets the appropriate headers, rather than just giving a URL directly to the file. The endpoint would accept the name of the file, or a unique ID for it (e.g. if your files are logged in a database) by which it could identify the correct file to retrieve. This also allows you to do proper security checking as well, so only authorised users can download a file - that's hard if your files are directly accessible to download. – ADyson Jun 14 '18 at 10:09

0 Answers0