在Node.js中将文件下载到缓冲区中,可以使用以下步骤:
http
模块:const http = require('http');
http.get('http://example.com/file-to-download', (response) => {
response.pipe(file);
});finish
事件,以确保文件下载完成:file.on('finish', () => {
console.log('文件下载完成。');
});完整的示例代码如下:
const fs = require('fs');
const https = require('https');
const file = fs.createWriteStream('path/to/destination/file');
https.get('https://example.com/file-to-download', (response) => {
response.pipe(file);
});
file.on('finish', () => {
console.log('文件下载完成。');
});
这样,文件将被下载并保存到指定的目标路径中。请注意,上述示例中的URL和目标文件路径仅作为示例,你需要根据实际情况进行修改。
领取专属 10元无门槛券
手把手带您无忧上云