【问题标题】:Multer Grid Fs Storage downloading instead of displaying video in expressMulter Gridfs 存储下载而不是快速显示视频
【发布时间】:2021-02-24 07:05:52
【问题描述】:

我正在关注 Travesy Media 关于 MongoDB gridfs 文件上传的教程。因此,当我尝试使用 readstream 在网络浏览器中显示文件时,它会下载视频而不是显示它! 这是我对该函数的代码:

router.get('/get_one/:filename', (req, res) => {
  gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
    // Check if file
    if (!file || file.length === 0) {
      return res.status(404).json({
        err: 'No file exists'
      });
    }

    // Check if image
    if (file.contentType === 'image/jpg' || file.contentType === 'video/mkv' || file.contentType === 'video/mp4' || file.contentType === "video/wmv" || file.contentType === "video/avi" || file.contentType === "video/flw") {
      // Read output to browser
      const readstream = gfs.createReadStream(file.filename);
      readstream.pipe(res);
    } else {
      res.status(404).json({
        err: 'Not an image'
      });
    }

请帮助我!我不知道如何解决这个问题! PS:file.contentType === "image/jpg" 是我试过的东西

【问题讨论】:

    标签: node.js mongodb multer-gridfs-storage


    【解决方案1】:
    • 记录 file.contentType 并找出您是否获得“filetype/fileExtension”,例如 image/jpeg、video/mp4 以及是否没有。只需在文件扩展名上使用正则表达式设置它

    • 尝试在将流传输到响应之前在响应中设置 Content-type 标头;

        const readstream = gfs.createReadStream(file.filename);
          res.set('Content-Type',file.contentType)
          readstream.pipe(res);
    

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2018-12-26
      • 2019-02-27
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多