node.js axios download file stream and writeFile

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

    This is my example code run with node js There is a synctax error

    should be writeFile not WriteFile

    const axios = require('axios');
    const fs = require('fs');
    axios.get('http://www.africau.edu/images/default/sample.pdf', {responseType: 'blob'}).then(response => {
      fs.writeFile('./my.pdf', response.data, (err) => {
            if (err) throw err;
            console.log('The file has been saved!');
        });
    });
    
    

    After the file is saved it might look like in a text editor, but the file was saved properly

    %PDF-1.3
    %����
    
    1 0 obj
    <<
    /Type /Catalog
    /Outlines 2 0 R
    /Pages 3 0 R
    >>
    endobj
    
    2 0 obj
    <<
    /Type /Outlines
    /Count 0
    >>
    endobj
    
    3 0 obj
    <<
    /Type /Pages
    /Count 2
    /Kids [ 4 0 R 6 0 R ] 
    >>
    endobj
    

提交回复
热议问题