[英]how to upload Image as multipart/form-data from android to Nodejs (gridfs is the way i am storing images)
[英]How can I compress an image before storing it in MongoDB using GridFS and NodeJS?
我正在嘗試使用 GridFS 和 MongoDB 在我的 NodeJS 應用程序中上傳圖像。 我想在將圖像上傳到 MongoDB 之前對其進行壓縮,並且我已經共享了 GridFS 中間件。
我想知道我應該把壓縮代碼放在哪里,或者是否有更好的方法來壓縮圖像?
require('dotenv').config();
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const Mongoose = require('mongoose');
const Mongouri = process.env.MONGO_URL;
let conn = Mongoose.connection;
let gfs;
conn.once('open',()=>{
gfs = Grid(conn.db,Mongoose.mongo);
gfs.collection('Posts');
});
//defining the middleware for multer so this will be called everytiime a new image is uploaded using multer
let storage = new GridFsStorage({
url:Mongouri,
file:(req,file)=>{
return new Promise(
(resolve,reject)=>{
const fileInfo = {
filename:Mongoose.Types.ObjectId()+file.originalname,
bucketName:"Posts"
};
resolve(fileInfo);
}
);
}
});
//initialising multer with the middleware
const upload = multer({storage});
module.exports = upload;
Sharp 是在將圖像保存到數據庫之前壓縮圖像的更好工具。 來源-夏普npm模塊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.