我第一次在Node.js和Multer库中处理多个文件.我在文件中有多个路径.例如,员工信息、产品等.有些路径只有一个字段用于输入文件,但有些路径有两个以上的字段用于输入文件.
条件句:
1. I should handle the files into the best suitable directory,
2. Rename each file to unique name in the directory.
3. Return each new name to the main function to record into database and reusable.
这就是我所try 的.我还附上了一个链接到git项目,包括请求图像的例子.
Git项目: https://github.com/Kicks-Me/multer-file-handle.git个
import * as multer from 'multer';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const mimeTypes = {
"image/png":"png",
"image/jpeg":"jpg",
"image/jpg":"jpg",
"image/gif":"gif",
"video/mp4":"mp4",
"audio/mpeg":"mp3",
"audio/wav":"wav",
"application/pdf":"pdf",
"application/vnd.ms-excel":"xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":"xlsx"
};
const accessFilePath = (fileType, reqpath) => {
let dest = "";
let pathType = "uploads/Others";
if(reqpath === "/location")
{
pathType = "uploads/locations";
}
else if(reqpath === "/employee")
{
pathType = "uploads/employees";
}
else if(reqpath === "/product")
{
pathType = "uploads/products";
}
dest =
mimeTypes[fileType] === "mp4"
? path.join(__dirname, "..", pathType, "video")
: mimeTypes[fileType] === "pdf"
? path.join(__dirname, "..", pathType, "documents", "pdf")
: mimeTypes[fileType] === "xls" || mimeTypes[fileType] === "xlsx"
? path.join(__dirname, "..", pathType, "documents", "excel")
: mimeTypes[fileType] === "mp3" || mimeTypes[fileType] === "wav"
? path.join(__dirname, "..", pathType, "audio")
: mimeTypes[fileType] === "gif"
? path.join(__dirname, "..", pathType, "gif")
: path.join(__dirname, "..", pathType, "image");
return dest;
};
const multerConfig = {
storage: multer.diskStorage({
destination: (req, file, callback) => {
let dest = "";
const fileType = file.mimetype;
dest = accessFilePath(fileType, req?.path);
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
callback(null, dest);
},
filename: (req, file, callback) => {
const ext = mimeTypes[file.mimetype];
callback(null, `${uuidv4()}.${ext}`);
},
}),
fileFilter: (req, file, callback) => {
const ext = mimeTypes[file.mimetype];
ext === "png" ||
ext === "jpg" ||
ext === "gif" ||
ext === "wav" ||
ext === "mp3" ||
ext === "wav" ||
ext === "mp4" ||
ext === "pdf" ||
ext === "xls" ||
ext === "xlsx"
? callback(null, true)
: callback(null, false);
},
};
export const upload = multer.default(multerConfig);
// Middleware to handle multiple files with different keys
export const handleMultipleFiles = (req, res, next) => {
const newImages = {};
// Process files attached to 'Profile' key
if (req.file) {
newImages.profile = req.file.filename;
}
// Process files attached to 'qr' key
if (req.files && req.files.length > 0) {
newImages.qr = req.files.map((file) => file.filename);
}
req.newImages = newImages;
next();
};
这是路由
import Route from 'express';
import { verifyJWT } from '../helper/jwt.js';
import { handleMultipleFiles, upload } from '../helper/multer.js';
const route = Route();
route.post('/upload',verifyJWT, upload.single('Profile'), upload.array('Images', 5),
handleMultipleFiles, (req, res) => {
const profileImage = req.newImages.profile;
const qrImages = req.newImages.qr;
return res.json({ profileImage, qrImages });
});
export default route;
在上面的代码中,我遇到了一些错误:
MulterError: Unexpected field
at wrappedFileFilter (/Users/tplussmacbookpro2/Desktop/kkkk/file-
handle/node_modules/multer/index.js:40:19)
at Multipart.<anonymous> (/Users/tplussmacbookpro2/Desktop/kkkk/file-
handle/node_modules/multer/lib/make-middleware.js:107:7)
at Multipart.emit (node:events:514:28)
at HeaderParser.cb (/Users/tplussmacbookpro2/Desktop/kkkk/file-
handle/node_modules/busboy/lib/types/multipart.js:358:14)
你可以在github上查看附加的项目以了解更多细节.
提前谢谢您.