GridFS 通过文件名下载_id 查找文件

GridFS find file by _id download with the name of the file

我想 post 使用 multer 和 gridfs express/mongodb 中的一个文件。

这个文件在前端被 link 编辑,如果我点击它我想显示文件的真实名称,例如 "example.pdf" 而不是 :id.pdf.现在 posting 工作完全正常我也可以保存原始文件名,但是当通过单击 link 获取文件时,文件名不再是原始文件名。即使我之前尝试设置它。

下面是我的get和post的两个例程:

var router = require('express').Router();
var mongoose = require('mongoose');
var conn = mongoose.connection;
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var fs = require('fs');

router.post('/', function(req, res) {
  var dirname = require('path').dirname(__dirname);
  var filename = req.file.filename;
  var path = req.file.path;

  var type = req.file.mimetype;
  var read_stream =  fs.createReadStream(dirname + '/' + path);

  var writestream = gfs.createWriteStream({
      _id:  filename,
     'filename': req.file.originalname,
     mode: 'w',
     content_type: type
  });
  read_stream.pipe(writestream);

  writestream.on('close', function (file) {
     res.status(200).json({'filename': filename});
   });
});

router.get('/:id',function(req , res) {
  var file_id = req.params.id;

  gfs.files.find({_id: file_id}).toArray(function (err, files) {
    if (err) {
      res.json(err);
    }
    if (files.length > 0) {
      var mime = files[0].contentType;
      var filename = files[0].filename;
      res.set('Content-Type', mime);
      res.set('originalname', filename);
      var read_stream = gfs.createReadStream({_id: file_id});
      read_stream.pipe(res);
    } else {
      res.json('File Not Found');
    }
  });
});

module.exports = router;

我设法解决了我的问题,为了获得正确的文件名,我必须在响应中设置一些内容,Whosebug 上的这个问题进一步解释了它:

related question

我想到了然后稍微修改了我的代码就变成了这样,这里是改进后的代码:

var router = require('express').Router();
var mongoose = require('mongoose');
var conn = mongoose.connection;
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var fs = require('fs');

router.post('/', function(req, res) {
  var dirname = require('path').dirname(__dirname);
  var filename = req.file.filename;
  var path = req.file.path;
  var type = req.file.mimetype;
  var read_stream =  fs.createReadStream(dirname + '/' + path);

  var writestream = gfs.createWriteStream({
      _id:  filename,
    'filename': req.file.originalname,
     mode: 'w',
     content_type: type
  });
  read_stream.pipe(writestream);

  writestream.on('close', function (file) {
     res.status(200).json({'filename': filename});
   });
});

router.get('/:id',function(req , res) {
  var file_id = req.params.id;

  gfs.files.find({_id: file_id}).toArray(function (err, files) {
    if (err) {
      res.json(err);
    }
    if (files.length > 0) {
      var mime = files[0].contentType;
      var filename = files[0].filename;
      res.set('Content-Type', mime);
      res.set('Content-Disposition', "inline; filename=" + filename);
      var read_stream = gfs.createReadStream({_id: file_id});
      read_stream.pipe(res);
    } else {
      res.json('File Not Found');
    }
  });
});

module.exports = router;

希望这对同样因为同样原因卡住的人有所帮助,我要感谢之前阅读问题的人。