我在將圖像文件上傳到我的服務器時遇到問題,無論出于何種原因,我都收到錯誤:(“TypeError:無法讀取未定義的屬性'路徑'”)。我在谷歌上搜索了這個錯誤,發現有些人遇到了同樣的問題,我試圖像他們一樣解決它,但它對我不起作用。這是我的代碼:const multer = require('multer');const storage = multer.diskStorage({? destination: function(req, file, cb) {? ? cb(null, './public/images/profilePictures');? },? filename: function(req, file, cb) {? ? cb(null, new Date().toISOString() + file.originalname);? }});const fileFilter = (req, file, cb) => {? // reject a file? if (file.mimetype === 'image/jpg' || file.mimetype === 'image/png') {? ? cb(null, true);? } else {? ? cb(null, false);? }};const upload = multer({? storage: storage,? limits: {? ? fileSize: 1024 * 1024 * 5? },? fileFilter: fileFilter});app.use(express.static('public'))映像架構和模型:const imageSchema = new mongoose.Schema({? ? profilePicture: String})const Image = new mongoose.model('Image', imageSchema)我的帖子路線:app.post('/changeProfilePic', upload.single('profilePicture'), function(req, res, next){? ? console.log(req.file);? ?const newImage = new Image({? ? ? ?profilePicture: req.file.path? ?})? ?newImage.save()})我的html上傳表格:<form action="/changeProfilePic" method="POST" enctype = "multipart/form-data">? ? ? <input type="file" name="profilePicture" placeholder="Image" />? ? ? <button class="btn btn-light btn-lg" type="submit">Upload</button>? ? </form>當我記錄 (req.file) 的值時,它說它的類型是“未定義”,所以這一定意味著 multer 沒有識別甚至沒有收到圖像文件。我做錯了什么,Multer 沒有得到文件?
添加回答
舉報
0/150
提交
取消