node.js axios download file stream and writeFile

前端 未结 8 963
花落未央
花落未央 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:26

    // This works perfectly well! 
    const axios = require('axios'); 
    
    axios.get('http://www.sclance.com/pngs/png-file-download/png_file_download_1057991.png', {responseType: "stream"} )  
    .then(response => {  
    // Saving file to working directory  
        response.data.pipe(fs.createWriteStream("todays_picture.png"));  
    })  
        .catch(error => {  
        console.log(error);  
    });  
    

提交回复
热议问题