【问题标题】:gridfs-stream always stores files with "contentType: binary/octet-stream"gridfs-stream 总是用“contentType: binary/octet-stream”存储文件
【发布时间】:2016-07-06 19:58:59
【问题描述】:

我正在尝试在 nodejs 中编写一个程序,该程序将文件列表存储在 mongodb 中。它工作正常,但有一个问题:它总是将 contentType 元数据存储为二进制/八位字节流,我希望它存储实际的 mime 类型。我尝试在 readStream 之前获取 mime 类型(通过 promises),但即使我硬编码 contentType(例如“image/jpeg”),它总是将元数据保存为“binary/octet-stream”。

这是我的代码:

files.forEach(function(f) {

    var conn = mongoose.createConnection(db);

    conn.once('open', function () {
      var gfs = Grid(conn.db);
      var writeStream = gfs.createWriteStream({
        filename: f.location,
        mode: 'w',
        contentType: 'image/jpeg'
      });

      writeStream.on('finish', function() {
          console.log('Storing', f.location, 'OK');
          return;
      })
      .on('error', function(data) {
          console.log('Error', f.location, data) 
      });

      fs.createReadStream(path.join(__dirname, 'files', f.location), 'utf-8')
      .pipe(writeStream);

    });
});

有什么想法吗?感谢您的回复!

【问题讨论】:

    标签: node.js mongodb mime-types gridfs gridfs-stream


    【解决方案1】:

    为了设置内容类型,您需要这样做:

    var writeStream = gfs.createWriteStream({
      filename: f.location,
      mode: 'w',
      content_type: 'image/jpeg'
    });
    

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2023-03-24
      • 1970-01-01
      • 2015-04-06
      • 2016-03-13
      • 2018-06-11
      • 2018-10-05
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多