我有一个 render.com 的付费帐户,并且我创建了一个磁盘,安装在 /uploads 上,这是我的 multer 配置
import multer from "multer";
import fs from "fs";
import path from "path";
function createFileUploader(dirName, maxFileSize, fileFilterLogic) {
const storage = multer.diskStorage({
destination: (req, file, callback) => {
const dir = path.resolve("/uploads", dirName);
const cleanedDir = dir.replace(/^.*?[\\\/]/, "");
if (!fs.existsSync(cleanedDir)) {
try {
fs.mkdirSync(cleanedDir, { recursive: true }); // Create directory recursively
} catch (err) {
console.error("Error creating directory:", err);
return callback(err);
}
}
console.log(cleanedDir);
callback(null, cleanedDir);
},
filename: (req, file, callback) => {
const extension = path.extname(file.originalname);
const filename = `${file.fieldname}-${Date.now()}${extension}`;
callback(null, filename);
},
});
const upload = multer({
storage: storage,
limits: {
fileSize: maxFileSize,
},`your text`
fileFilter: fileFilterLogic, // Apply the file filter logic here
});
return upload;
}
// Define the file filter logic function
export function fileFilterLogic(req, file, cb) {
// Check file type
if (
file.mimetype === "image/jpeg" ||
file.mimetype === "image/png" ||
file.mimetype === "application/pdf" ||
file.mimetype ===
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
) {
// Accept the file
cb(null, true);
} else {
// Reject the file
cb(new Error("File type not supported"));
}
}
// Example usage:
const cv = "./CVs";
const logo = "./logo";
const profileImg = "./profileImg";
const maxSize = 1024 * 1024 * 60; // 60MB
// Create the upload middleware with the provided parameters and file filter logic
export const uploadCv = createFileUploader(cv, maxSize, fileFilterLogic);
export const uploadLogo = createFileUploader(logo, maxSize, fileFilterLogic);
export const uploadProfileImg = createFileUploader(
profileImg,
maxSize,
fileFilterLogic
);
我也尝试了一些方法,但仍然不起作用。
我希望当用户上传文件时我能够下载它们,并且如果上传图像,图像将显示在网络浏览器上
我也遇到了同样的问题。你有没有得到任何解决方案,我认为使用云存储对于 multer 来说是更好的选择。