download file axios nodejs

Solutions on MaxInterview for download file axios nodejs by the best coders in the world

showing results for - "download file axios nodejs"
Jan
21 Sep 2019
1axios.get('https://xxx/my.pdf', {responseType: 'blob'}).then(response => {
2    fs.writeFile('/temp/my.pdf', response.data, (err) => {
3        if (err) throw err;
4        console.log('The file has been saved!');
5    });
6});
7
Mellina
14 Feb 2017
1export async function downloadFile(fileUrl: string, outputLocationPath: string) {
2  const writer = createWriteStream(outputLocationPath);
3
4  return Axios({
5    method: 'get',
6    url: fileUrl,
7    responseType: 'stream',
8  }).then(response => {
9
10    //ensure that the user can call `then()` only when the file has
11    //been downloaded entirely.
12
13    return new Promise((resolve, reject) => {
14      response.data.pipe(writer);
15      let error = null;
16      writer.on('error', err => {
17        error = err;
18        writer.close();
19        reject(err);
20      });
21      writer.on('close', () => {
22        if (!error) {
23          resolve(true);
24        }
25        //no need to call the reject here, as it will have been called in the
26        //'error' stream;
27      });
28    });
29  });
30}
31This way, you can call downloadFile(), call then() on the returned promise, and making sure that the downloaded file will have completed processing.
32
33Or, if you use a more modern version of NodeJS, you can try this instead:
34
35import * as stream from 'stream';
36import { promisify } from 'util';
37
38const finished = promisify(stream.finished);
39
40export async function downloadFile(fileUrl: string, outputLocationPath: string): Promise<any> {
41  const writer = createWriteStream(outputLocationPath);
42  return Axios({
43    method: 'get',
44    url: fileUrl,
45    responseType: 'stream',
46  }).then(async response => {
47    response.data.pipe(writer);
48    return finished(writer); //this is a Promise
49  });
50}
queries leading to this page
download file using axios node jsuse axios to download filejavascript download file axiosjavascript axios download filedownload file with axiosdownload file axiosaxios file download exampleaxios download a filehow to download a file axiosdownload file axios vanilla jsaxios get file nodejs download file after axiosfrontend axios download fileaxios post download filedownload file from url nodejs axiosaxios get download filenodejs axios fs download imageaxios download file nodeaxios to download fileaxios response download filedownload file from axiosdownload file node js with axiosdownload file nodejs axiosdownload files using axiosaxios node js upload filedownload file using axios node js and send it to frontendusing axios to download a fileaxios download file node jshow to download a file using axiosjs axios download filenodejs download file from url axioshow to make file download axiosnode js axios download filedownload file in axiosaxios get and download fileaxios get download filedonwload file with axiosjs axios download response fileaxios download image nodejsjs download file axiosaxios http download filenodejs axios download image responseaxios dowloadfiledownload javascript file axiosaxios to download file objectnode js axios get fileaxios file download nodejspost data and download file nodejs axiosdownload a file with axiosdownload with nodejs to directory axiosaxios method for download filejs download file from url axios axios donwload fileaxios get file donwloaddownload files from axiosdownload file axios and expressdownload files axiosdownload a file using axioshow to download a file through axiosaxios browser download filedownload file using axios axios download filedownload axios filedownload file axios nodejsdownload axios downloadnodejs download file fs axiosnodejs download file axiosaxios download 27 2c 27file from url nodejsaxios download file browserfile downloader axios node jsdownload a file axioshow to download file with axiosdownloading file with axiosdownload files with axiosjavascript axios download fileaxios nodejs download fileaxios nodejs file downloadcan axios get from local fileaxios download filesimple axios file downloaddownload a file with axios node jshow put download file in axiosaxios download file nodejsaxios file downloaddownload file axios nodejs