node.js axios download file stream and writeFile

前端 未结 8 989
花落未央
花落未央 2020-12-24 08:50

i want download a pdf file with axios and save on disk (server side) with fs.writeFile, i have tried:

axios.get(\'https://xxx/my.pd         


        
8条回答
  •  借酒劲吻你
    2020-12-24 09:32

    node fileSystem writeFile encodes data by default to UTF8. which could be a problem in your case.

    Try setting your encoding to null and skip encoding the received data:

    fs.writeFile('/temp/my.pdf', response.data, {encoding: null}, (err) => {...}
    

    you can also decalre encoding as a string (instead of options object) if you only declare encoding and no other options. string will be handled as encoding value. as such:

    fs.writeFile('/temp/my.pdf', response.data, 'null', (err) => {...}
    

    more read in fileSystem API write_file

提交回复
热议问题