1、使用前引入 axios
2、responseType: ‘arraybuffer’
// 网络文件下载
async downloadFile(uri, filename) {
await axios({
url: 'http://cdn.z114.com/dbzWallpaper/' + uri,
responseType: 'arraybuffer'
}).then((res) => {
console.log(res)
let dataBuffer = this.toBuffer(res.data)
let localPath = exePath + '\\resources\\wallpaper\\'
fs.writeFile(localPath + filename, dataBuffer, () => {
console.log('保存完毕')
});
}).catch((error) => {
console.log('异常代码 => ', error.response.status)
});
},
// ArrayBuffer ---> Buffer
toBuffer(ab) {
var buf = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buf.length; ++i) {
buf[i] = view[i];
}
return buf;
}