0

I am using request js to download a file.

function requ(){
    const options = {
        uri: `api/tasks/${id}/attachments/${attachmentId}`
    }
    return rp.get(options)
 }

My question is:

why piping to "res" like requ().pipe(res) works and returning the result of the request above using "send" like

requ().then((result)=>{
    //here result is the file's representing string
    res.send(result)

})

don't?

2 Answers2

0
const fs = require('fs');
requ().then((result) => {
    //here result is the file's representing string
    const path = __dirname + '/tempFiles' + Date.now(); // a temporary file to send it 
    fs.writeFile(path, result, function(err) {
        if(err) throw err;
        return res.sendFile(path);
    })
});

Read More About fs, link 2

Jamal Abo
  • 472
  • 4
  • 16
0

My file was being corrupted because request was converting the response body to utf8. Using:

const options = {
    uri: `api/tasks/${id}/attachments/${attachmentId}`,
    encoding:null
}

fixed the problem