显示图像时如何调整使用GridFS存储的图像的大小

问题描述

这些是

下路线的变量
const GridFsstorage = require("multer-gridfs-storage");
const Grid = require("gridfs-stream");
var conn = mongoose.createConnection(db,{
  useNewUrlParser: true,useUnifiedTopology: true,});
let gfs;
conn.once("open",() => {
  gfs = Grid(conn.db,mongoose.mongo);
  gfs.collection("uploads");
});

这是我目前正在读取文件的方式

router.get("/image/:filename",(req,res) => {
  gfs.files.findOne({ filename: req.params.filename },(err,file) => {
    if (!file || file.length === 0) {
      return res.status(404).json({
        err: "No file exist",});
    }
    //check if it's a jpeg
    if (
      file.contentType === "image/jpeg" ||
      file.contentType === "img/png" ||
      file.contentType === "image/png"
    ) {
      //read output to browser
      const readstream = gfs.createReadStream(file.filename);
      readstream.pipe(res);
    } else {
      res.status(404).json({
        err: "Not an image",});
    }
  });
});

如果可以的话,使用任何外部模块在上述路线中显示图像时如何调整图像大小,或者也可以接受其他解决方

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)