node.js axios download file stream and writeFile

Javascriptnode.jsAxios

Javascript Problem Overview


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.pdf', {responseType: 'blob'}).then(response => {
    fs.writeFile('/temp/my.pdf', response.data, (err) => {
        if (err) throw err;
        console.log('The file has been saved!');
    });
});

the file is saved but the content is broken...

how do I correctly save the file?

Javascript Solutions


Solution 1 - Javascript

Actually, I believe the previously accepted answer has some flaws, as it will not handle the writestream properly, so if you call "then()" after Axios has given you the response, you will end up having a partially downloaded file.

This is a more appropriate solution when downloading slightly larger files:

export async function downloadFile(fileUrl: string, outputLocationPath: string) {
  const writer = createWriteStream(outputLocationPath);

  return Axios({
    method: 'get',
    url: fileUrl,
    responseType: 'stream',
  }).then(response => {

    //ensure that the user can call `then()` only when the file has
    //been downloaded entirely.

    return new Promise((resolve, reject) => {
      response.data.pipe(writer);
      let error = null;
      writer.on('error', err => {
        error = err;
        writer.close();
        reject(err);
      });
      writer.on('close', () => {
        if (!error) {
          resolve(true);
        }
        //no need to call the reject here, as it will have been called in the
        //'error' stream;
      });
    });
  });
}

This way, you can call downloadFile(), call then() on the returned promise, and making sure that the downloaded file will have completed processing.

Or, if you use a more modern version of NodeJS, you can try this instead:

import * as stream from 'stream';
import { promisify } from 'util';

const finished = promisify(stream.finished);

export async function downloadFile(fileUrl: string, outputLocationPath: string): Promise<any> {
  const writer = createWriteStream(outputLocationPath);
  return Axios({
    method: 'get',
    url: fileUrl,
    responseType: 'stream',
  }).then(response => {
    response.data.pipe(writer);
    return finished(writer); //this is a Promise
  });
}

Solution 2 - Javascript

You can simply use response.data.pipe and fs.createWriteStream to pipe response to file

axios({
	method: "get",
	url: "https://xxx/my.pdf",
	responseType: "stream"
}).then(function (response) {
	response.data.pipe(fs.createWriteStream("/temp/my.pdf"));
});

Solution 3 - Javascript

// 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);  
});  

Solution 4 - Javascript

The problem with broken file is because of backpressuring in node streams. You may find this link useful to read: https://nodejs.org/es/docs/guides/backpressuring-in-streams/

I'm not really a fan of using Promise base declarative objects in JS codes as I feel it pollutes the actual core logic & makes the code hard to read. On top of it, you have to provision event handlers & listeners to make sure the code is completed.

A more cleaner approach on the same logic which the accepted answer proposes is given below. It uses the concepts of stream pipelines.

const util = require('util');
const stream = require('stream');
const pipeline = util.promisify(stream.pipeline);

const downloadFile = async () => {
  try {
    const request = await axios.get('https://xxx/my.pdf', {
      responseType: 'stream',
    });
    await pipeline(request.data, fs.createWriteStream('/temp/my.pdf'));
    console.log('download pdf pipeline successful');   
  } catch (error) {
    console.error('download pdf pipeline failed', error);
  }
}

exports.downloadFile = downloadFile

I hope you find this useful.

Solution 5 - Javascript

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

Solution 6 - Javascript

I have tried, and I'm sure that using response.data.pipe and fs.createWriteStream can work.


Besides, I want to add my situation and solution

Situation:

  • using koa to develop a node.js server
  • using axios to get a pdf via url
  • using pdf-parse to parse the pdf
  • extract some information of pdf and return it as json to browser

Solution:

const Koa = require('koa');
const app = new Koa();
const axios = require('axios')
const fs = require("fs")
const pdf = require('pdf-parse');
const utils = require('./utils')

app.listen(process.env.PORT || 3000)

app.use(async (ctx, next) => {
	  let url = 'https://path/name.pdf'
      let resp = await axios({
		  url: encodeURI(url),
		  responseType: 'arraybuffer'
		})

		let data = await pdf(resp.data)

		ctx.body = {
			phone: utils.getPhone(data.text),
			email: utils.getEmail(data.text),
		}
})

In this solution, it doesn't need to write file and read file, it's more efficient.

Solution 7 - Javascript

This is what worked for me and it also creates a temporary file for the image file in case the output file path is not specified:

const fs = require('fs')
const axios = require('axios').default
const tmp = require('tmp');

const downloadFile = async (fileUrl, outputLocationPath) => {
    if(!outputLocationPath) {
        outputLocationPath = tmp.fileSync({ mode: 0o644, prefix: 'kuzzle-listener-', postfix: '.jpg' });
    }
    let path = typeof outputLocationPath === 'object' ? outputLocationPath.name : outputLocationPath
    const writer = fs.createWriteStream(path)
    const response = await axios.get(fileUrl, { responseType: 'arraybuffer' })
    return new Promise((resolve, reject) => {
        if(response.data instanceof Buffer) {
            writer.write(response.data)
            resolve(outputLocationPath.name)
        } else {
            response.data.pipe(writer)
            let error = null
            writer.on('error', err => {
                error = err
                writer.close()
                reject(err)
            })
            writer.on('close', () => {
                if (!error) {
                    resolve(outputLocationPath.name)
                }
            })
        }
    })
}

Here is a very simple Jest test:

it('when downloadFile should downloaded', () => {
    downloadFile('https://i.ytimg.com/vi/HhpbzPMCKDc/hq720.jpg').then((file) => {
        console.log('file', file)
        expect(file).toBeTruthy()
        expect(file.length).toBeGreaterThan(10)
    })
})

Solution 8 - Javascript

The following code taken from https://gist.github.com/senthilmpro/072f5e69bdef4baffc8442c7e696f4eb?permalink_comment_id=3620639#gistcomment-3620639 worked for me

const res = await axios.get(url, { responseType: 'arraybuffer' });
fs.writeFileSync(downloadDestination, res.data);

Solution 9 - Javascript

import download from "downloadjs";

export const downloadFile = async (fileName) => {
    axios({
        method: "get",
        url: `/api/v1/users/resume/${fileName}`,
        responseType: "blob",
    }).then(function (response) {
        download(response.data, fileName);
    });
};

it's work fine to me

Solution 10 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionar099968View Question on Stackoverflow
Solution 1 - JavascriptcsotiriouView Answer on Stackoverflow
Solution 2 - Javascriptponury-kostekView Answer on Stackoverflow
Solution 3 - JavascriptArmandView Answer on Stackoverflow
Solution 4 - JavascriptAman SarafView Answer on Stackoverflow
Solution 5 - JavascriptfedescView Answer on Stackoverflow
Solution 6 - Javascriptlevy9527View Answer on Stackoverflow
Solution 7 - Javascriptgil.fernandesView Answer on Stackoverflow
Solution 8 - JavascriptLorenzo RegaladoView Answer on Stackoverflow
Solution 9 - JavascriptArman SeptianView Answer on Stackoverflow
Solution 10 - JavascriptKhoa Tran dangView Answer on Stackoverflow