0

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 文件上传代码不起作用 文件下载代码运行良好。如何在路径中设置 readstream 文件的路径。

4

1 回答 1

0

busboy从不直接写入磁盘。你得到的只是一个普通的可读流(file),所以把它传送到你的writeStream

file.pipe(writeStream);

代替:

fs.createReadStream('/some/path').pipe(writestream);

此外,您的if (!file)检查将永远不会执行,因为file始终被定义,并且console.log('File [' + fieldname + '] got ' + data.length + ' bytes');是错误的,因为该范围内没有data变量。

于 2015-08-02T20:43:25.100 回答