问题描述
以下行将从指定的 url
变量下载图像文件:
The following line will download an image file from a specified url
variable:
var filename = path.join(__dirname, url.replace(/^.*[\/]/, ''));
request(url).pipe(fs.createWriteStream(filename));
这些行将获取该图像并保存到 MongoDB GridFS:
And these lines will take that image and save to MongoDB GridFS:
var gfs = Grid(mongoose.connection.db, mongoose.mongo);
var writestream = gfs.createWriteStream({ filename: filename });
fs.createReadStream(filename).pipe(writestream);
像这样链接 pipe
会抛出 错误:500 无法管道.不可管道化.
Chaining pipe
like this throws Error: 500 Cannot Pipe. Not Pipeable.
request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);
发生这种情况是因为图像文件还没有准备好被读取,对吗?我应该怎么做才能解决这个问题?错误:500 无法管道.不可管道化.
This happens because the image file is not ready to be read yet, right? What should I do to get around this problem?Error: 500 Cannot Pipe. Not Pipeable.
使用以下内容:Node.js 0.10.10、mongoose、request 和 gridfs-stream 库.
Using the following: Node.js 0.10.10, mongoose, request and gridfs-stream libraries.
推荐答案
request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);
与此相同:
var fileStream = fs.createWriteStream(filename);
request(url).pipe(fileStream);
fileStream.pipe(writestream);
所以问题是你试图将.pipe
一个WriteStream
导入另一个WriteStream
.
So the issue is that you are attempting to .pipe
one WriteStream
into another WriteStream
.
这篇关于如何在 Node.js 0.10 中立即使用读取流链接写入流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!