Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are using this code to download the pdf file but we are getting problem. i want when i hit this route then pdf file will download.
But when i hit this api in postman and click on the download button then pdf file download. So i wnat to download the pdf file with data.

What I have tried:

router.get('/demo',function (req,res){
async function getinvoicelist(){

var chunks = [];
var options = { method: 'GET',
url: 'https://api.fortnox.se/3/invoices/31/preview',

request(options, function (error, response, body) {

console.log(body);

});

%PDF-1.4
%����
3 0 obj
<</Length 1438/Filter/FlateDecode>>stream
x��Y�r�6��+�Ɍ3S�xXڭ�h���IM���STB�n>�_�/p~!�^<H��N�xti���}��T�, ��&k��g���xU|*҂�Gk�JPSH#13�װ�).�?�����`�������/��
Posted
Updated 25-Oct-18 7:48am
Comments
Mohibur Rashid 24-Oct-18 22:41pm    
What's the point of posting pdf file content?

1 solution

Downloading files via AJAX requests is tricky. You might be able to use createObjectURL to do this:
JavaScript
request(options, function (error, response) {
    var blob = new Blob([response], { type: 'application/pdf' });
    var url = window.URL.createObjectURL(blob);
    
    var link = document.createElement('a');
    link.href = url;
    link.download = "Preview.pdf";

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    // Recommended: revoke the object URL after some time to free up resources.
    // There is no way to know when the download is complete.
    setTimeout(function() { window.URL.revokeObjectURL(url); }, 60000);
});

Downloading files from Ajax POST Requests[^]
Javascript - Downloading Files with AJAX and Showing a Progess Bar[^]

Can I use... createObjectURL[^]
Can I use... Blob[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900