Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js url download

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

PREVIOUS NEXT
Code Example
Javascript :: add google analytics to react 
Javascript :: update map value javascript 
Javascript :: nested function javascript 
Javascript :: sublime javascript autocomplete 
Javascript :: js append to array 
Javascript :: is undefined false in javascript 
Javascript :: express session 
Javascript :: cancel or abort axios request 
Javascript :: pattern alphabet and space 
Javascript :: js variable 
Javascript :: how to check if input is string javascript 
Javascript :: how to setup icomoon in react js 
Javascript :: remove spaces from string javascript 
Javascript :: base 2 number javascript 
Javascript :: map function react 
Javascript :: forceupdate usereducer 
Javascript :: model mongoose 
Javascript :: removes null and false values from an array 
Javascript :: split string to char js 
Javascript :: javascript getters and setters 
Javascript :: get json data into array of object 
Javascript :: what are json files for 
Javascript :: comparing two array of objects in javascript returning differences 
Javascript :: react native shadow android 
Javascript :: ssg full form in nextjs 
Javascript :: javascript move array element to front 
Javascript :: get attribute js 
Javascript :: how to convert draftjs content to html 
Javascript :: javascript https post 
Javascript :: change image onclick javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =