fs 模块 writeFileStream 实际上没有在磁盘上创建文件
fs module writeFileStream not actually creating file on disk
正在开发一个小型文件传输实用程序来取代旧的基于电子邮件的订单处理系统,为此我正在使用 Nodejs、express 和许多其他库。
目前的问题是我已经把数据拉过来了,但最后似乎无法真正将文件保存到磁盘。
var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
res.on('data', (data)=>{
console.log(data.toString());
fileStream.write(data);
})
.on('end',()=>{
fileStream.close();
fileStream.end();
});
});
这是我一直在使用的代码,它只是一个片段。假设所有变量都已设置且类型正确,正如我在这里确保的那样。
据我了解,fileStream.end()
函数应该关闭流并将文件保存到磁盘,但它并没有这样做。我查看了它应该在的文件夹,但什么也没有。
还有更多信息,这是我的配置对象:
module.exports = {
location: 'CA',
watch:{
folder: './watch/',
transUrl: 'http://localhost:3289',
transmission: 'send'
},
poll:{
folder: './recieve',
transUrl: 'http://localhost:3289',
transmission: 'receive'
}
}
最终找到解决方法:
最终代码忽略了在数据传入时添加到流中的概念,因为在当前实现中数据是纯文本。
最终代码如下:
var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var fileContent = '';
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
res.on('data', (data)=>{
fileContent += data.toString();
})
.on('end',()=>{
fs.writeFile(path.join(download_path, filename), fileContent,(err) =>{
if(err){
return console.error(err);
}
console.log('file was saved')
})
});
});
正确的做法是 pipe
:
http.get(file_url, (res) => {
const filePath = path.join(download_path, filename)
const writeStream = fs.createWriteStream(filePath)
res.pipe(writeStream)
.on('error', (e) => console.error(e))
.on('close', () => console.log(`file was saved to ${filePath}`))
})
正在开发一个小型文件传输实用程序来取代旧的基于电子邮件的订单处理系统,为此我正在使用 Nodejs、express 和许多其他库。
目前的问题是我已经把数据拉过来了,但最后似乎无法真正将文件保存到磁盘。
var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
res.on('data', (data)=>{
console.log(data.toString());
fileStream.write(data);
})
.on('end',()=>{
fileStream.close();
fileStream.end();
});
});
这是我一直在使用的代码,它只是一个片段。假设所有变量都已设置且类型正确,正如我在这里确保的那样。
据我了解,fileStream.end()
函数应该关闭流并将文件保存到磁盘,但它并没有这样做。我查看了它应该在的文件夹,但什么也没有。
还有更多信息,这是我的配置对象:
module.exports = {
location: 'CA',
watch:{
folder: './watch/',
transUrl: 'http://localhost:3289',
transmission: 'send'
},
poll:{
folder: './recieve',
transUrl: 'http://localhost:3289',
transmission: 'receive'
}
}
最终找到解决方法:
最终代码忽略了在数据传入时添加到流中的概念,因为在当前实现中数据是纯文本。
最终代码如下:
var file_url = `${config.poll.transUrl}/?location=${config.location}&transmission=${config.poll.transmission}`;
console.log(file_url);
var download_path = config.poll.folder;
var fileContent = '';
var filename = setFileName();
var fileStream = fs.createWriteStream(download_path + filename);
fileStream.on('finish', ()=>{
console.log(`${filename} has been downloaded to: ${download_path}`);
});
http.get(file_url, (res)=>{
res.on('data', (data)=>{
fileContent += data.toString();
})
.on('end',()=>{
fs.writeFile(path.join(download_path, filename), fileContent,(err) =>{
if(err){
return console.error(err);
}
console.log('file was saved')
})
});
});
正确的做法是 pipe
:
http.get(file_url, (res) => {
const filePath = path.join(download_path, filename)
const writeStream = fs.createWriteStream(filePath)
res.pipe(writeStream)
.on('error', (e) => console.error(e))
.on('close', () => console.log(`file was saved to ${filePath}`))
})