Guest User

Untitled

a guest
Jun 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. const axios = require('axios')
  2.  
  3. /**
  4. * Download url to a file on disk
  5. * @param url
  6. * @param destFile
  7. */
  8. async function downloadAxios(url: string, destFile: string) {
  9. const response = await axios({
  10. method: 'GET',
  11. url: url,
  12. responseType: 'stream' // axios image download
  13. })
  14.  
  15. response.data.pipe(fs.createWriteStream(destFile)) // pipe the result stream directly to a file
  16.  
  17. // return a promise
  18. return new Promise((resolve, reject) => {
  19. response.data.on('end', () => {
  20. resolve()
  21. })
  22. response.data.on('error', () => {
  23. reject()
  24. })
  25. })
  26. }
Add Comment
Please, Sign In to add comment