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

时间:2016-04-27 13:54:26

标签: javascript file express multer gridfs-stream

我想用multer和gridfs在express / mongodb中发布一个文件。

此文件在前端与a链接,如果我点击它,我想显示文件的真实姓名,例如“example.pdf”而不是:id.pdf。现在发布工作完全正常我也可以保存原始文件名,但是当通过点击链接获取文件时,文件名不再是原始文件名。即使我试着在之前设置它。

这是我的两个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;

1 个答案:

答案 0 :(得分:2)

我设法解决了我的问题,获取了我必须在响应中设置内容的文件的正确名称,stackoverflow上的这个问题进一步解释了这一点:

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;

我希望这能为那些因同样的原因而陷入困境的人提供一些帮助,我要感谢那些通过这个问题阅读过的人。