Created
June 5, 2018 20:09
-
-
Save tvaliasek/be0b54230ccfebb46a8c8654f368af71 to your computer and use it in GitHub Desktop.
electron axios stream download with progress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ipcMain} from 'electron' | |
const fs = require('fs') | |
const axios = require('axios') | |
/* ... */ | |
ipcMain.on('downloadFile', function (event, data) { | |
const filePath = data.filePath | |
const item = data.item | |
axios({ | |
method: 'GET', | |
url: item.download_link, | |
responseType: 'stream' | |
}).then((response) => { | |
response.data.pipe(fs.createWriteStream(filePath)) | |
const totalSize = response.headers['content-length'] | |
var downloaded = 0 | |
response.data.on('data', (data) => { | |
downloaded += Buffer.byteLength(data) | |
event.sender.send('downloadProgress', {total: totalSize, loaded: downloaded}) | |
}) | |
response.data.on('end', () => { | |
event.sender.send('downloadEnd') | |
}) | |
response.data.on('error', (error) => { | |
event.sender.send('downloadError', error) | |
}) | |
}).catch((error) => { | |
event.sender.send('downloadError', error) | |
}) | |
}) | |
/* ... */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Originally I used it in a vue component, "this" refers to the data property. "Item" is an object: | |
{ | |
"file_name": "some_file.zip", | |
"download_link": "http://some.location.com/some_file.zip" | |
} | |
*/ | |
import {ipcRenderer} from 'electron' | |
const fs = require('fs') | |
/* ... */ | |
getDownloadProgress: function () { | |
let progress = (this.totalSize > 0 && this.downloadedSize > 0) ? Math.ceil(this.downloadedSize / (this.totalSize / 100)) : 0 | |
return (progress > 100) ? 100 : progress | |
}, | |
download: function () { | |
const _this = this | |
const path = process.env.INIT_CWD + '/download' | |
const filePath = process.env.INIT_CWD + '/download/' + this.item.file_name | |
// check folder | |
if (!fs.existsSync(path)) { | |
fs.mkdirSync(path) | |
// delete file if exists | |
} else if (fs.existsSync(filePath)) { | |
fs.unlinkSync(filePath) | |
} | |
return new Promise((resolve, reject) => { | |
_this.isDownloading = true | |
ipcRenderer.send('downloadFile', { | |
item: _this.item, | |
filePath: filePath | |
}) | |
ipcRenderer.on('downloadProgress', (event, progressEvent) => { | |
_this.totalSize = progressEvent.total | |
_this.downloadedSize = progressEvent.loaded | |
}) | |
ipcRenderer.on('downloadEnd', () => { | |
ipcRenderer.removeAllListeners('downloadEnd') | |
ipcRenderer.removeAllListeners('downloadProgress') | |
ipcRenderer.removeAllListeners('downloadError') | |
resolve(filePath) | |
}) | |
ipcRenderer.on('downloadError', (event, error) => { | |
ipcRenderer.removeAllListeners('downloadEnd') | |
ipcRenderer.removeAllListeners('downloadProgress') | |
ipcRenderer.removeAllListeners('downloadError') | |
reject(error) | |
}) | |
}) | |
} | |
/* ... */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment