1

I created a PDF of a website using puppeteer from a link provided by the client, and now I need to send back as response the stream code/ octet stream / binary data of this pdf, I am unsure how to do that can someone help me

I have the PDF stored locally in the same folder as my js file

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
craft55
  • 93
  • 1
  • 6
  • 1
    Can you show us what you've tried so far? Just google *upload file with javascript* and look at one of those. – Peter Krebs Aug 11 '22 at 14:51
  • Are you asking what "Content-Type:" you need in the return HTTP headers or something else? – Barry Carter Aug 11 '22 at 15:11
  • @BarryCarter I am not sure how to send back the file as an octet stream, I suppose the content type is application/octet-stream right? I saw this code that uses Express.js but I use KOA so its not exactly working https://newbedev.com/javascript-how-to-send-application-octet-stream-express-code-example#:~:text=how%20to%20send%20application%2Foctet-stream%20%2B%20express%20code%20example,next%29%20%3D%3E%20%7B%20res.setHeader%28%27Content-Type%27%2C%20%27application%2Foctet-stream%27%29%3B%20res.sendFile%28%27%2Ffile%2Fto%2Fbe%2Fdownloaded.ext%27%29%3B%20res.status%28200%29%3B%20%7D%29%3B – craft55 Aug 11 '22 at 15:25
  • 1
    Wouldn't the file be of type applicatoin/pdf? – asdf3.14159 Aug 11 '22 at 16:18
  • @asdf3.14159 thats the thing I want it to be sent to the client as binary stream, help – craft55 Aug 11 '22 at 16:28

1 Answers1

0

Just use

response.body = fs.readFileSync("path/to/file.pdf");

in KOA.

This will automatically send the pdf file as an application/octet-stream.

asdf3.14159
  • 694
  • 3
  • 19