node.js - 如何使用gridfs nodeJS调整图像大小并存储不同的sizet?
问题描述
我是 mongoDB 和 gridfs 的初学者。我有一个在数据库中保存传入图像的代码。但我想在保存之前调整图像大小。它试图使用sharp,但我真的不明白它是如何工作的。
我尝试了以下代码,但它不起作用
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename = req.params.userID;
const fileInfo = {
filename: filename,
bucketName: "ProfilePic",
};
resolve(fileInfo);
});
}
});
const upload = multer({ storage });
router.post("/setProfilePic/:userID", upload.single("picture"), async(req, res) => {
//code not working
await sharp(req.file.path)
.resize(500)
.jpeg({quality: 50})
.toFile(
path.resolve(req.file.destination,'resized',image)
)
// end of non working code
return res.status(200).json({state: "ok"})
});
解决方案
推荐阅读
- cordova - Hammer.js 滑动不适用于 Cordova InAppBrowser
- r - 基于线斜率的线颜色
- azure-cognitive-search - 使用 Lucene 查询语法的 Azure 搜索返回不正确的结果
- vba - 438 对象不支持此属性或方法 vba
- javascript - 如何通过承诺链传递变量
- python - 在 For 循环中从 Windows 批处理文件中的数组访问项目
- javascript - c#方法中的重定向不起作用
- ios - Swift Annotation Pin 的 Calloutbubble 手势
- php - 未捕获的异常:无法找到混合文件
- c# - 获取 Collection CosmosDb 中的所有文档