Created
May 29, 2020 02:50
-
-
Save avaforvr/8a9c8458ad494afc31e9fa7ebf4e04e1 to your computer and use it in GitHub Desktop.
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
function downloadFileAsync(url, dest) { | |
return new Promise((resolve, reject) => { | |
const file = fs.createWriteStream(dest); | |
http.get(url, (res) => { | |
if (res.statusCode !== 200) { | |
reject(response.statusCode); | |
return; | |
} | |
// 进度、超时等 | |
file.on('finish', () => { | |
console.log('downloaded: ' + url); | |
file.close(resolve); | |
}).on('error', (err) => { | |
fs.unlink(dest); | |
reject(err.message); | |
}) | |
res.pipe(file); | |
}); | |
}); | |
} |
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
function readRemoteFileAsync(url) { | |
return new Promise((resolve, reject) => { | |
http.get(url, (res) => { | |
if (res.statusCode !== 200) { | |
reject(response.statusCode); | |
return; | |
} | |
let rawData = ''; | |
res.on('data', (chunk) => { | |
rawData += chunk; | |
}); | |
res.on('end', () => { | |
resolve(rawData); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment