[英]gridfs-stream upload a file into mongodb not working in node.js server code
gridfs-stream upload a file not working, but download a file working plz give solution gridfs-stream上传无法正常工作的文件,但下载正常工作的文件请给出解决方案
app.post('/file', function(req, res) {
var busboy = new Busboy({
headers: req.headers
});
var fileId = new mongo.ObjectID();
var file = filename();
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
if (!file) return res.send({
result: 'NO_FILE_UPLOADED This Error by not upload any file'
});
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
var writeStream = gfs.createWriteStream({
_id: fileId,
filename: my_file.txt,
mode: 'w',
chunkSize: 1024,
root: fs,
content_type: mimetype
});
// here how can i give a path to read stream ?
// File id is a mongodbid its ok. but what about file name
fs.createReadStream('/some/path').pipe(writestream);
}).on('finish', function() {
res.writeHead(200, {
'content-type': 'text/html'
});
res.end('<a href="http://localhost:49106/file/' + fileId.toString() + '">download file</a>');
});
req.pipe(busboy);//its working
});
gridfs-stream file upload code is not working file download code is working very well.how can i set the path of the readstream files in the path. gridfs-stream文件上传代码不起作用文件下载代码运行良好。如何在路径中设置readstream文件的路径。
busboy
never directly writes to disk. busboy
从不直接写入磁盘。 What you get is just a plain readable stream ( file
), so pipe that to your writeStream
: 您得到的只是一个普通可读的流(
file
),因此将其通过管道传递到writeStream
:
file.pipe(writeStream);
instead of: 代替:
fs.createReadStream('/some/path').pipe(writestream);
Also your if (!file)
check will never execute, because file
will always be defined, and console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
而且,
if (!file)
检查将永远不会执行,因为将始终定义file
,并且console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
is wrong because there is no data
variable in that scope. 错误的,因为在该范围内没有
data
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.