[英]How to display uploaded image using multer?
我使用 nodejs 作為后端,使用 vuejs 作為前端。 我已經使用郵遞員將一些文件作為測試上傳到我的后端,但是,如何使用 multer 顯示這些上傳的文件?
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
const upload = multer({ storage: storage }).single('logo');
router.post('/logo', auth.authorize, (req, res, next) => {
upload(req, res, (err) => {
});
res.status(200).send('uploaded');
});
我的 api 公共文件夾中有一個名為“logo-1531296527722”的圖像。 如果我嘗試在我的 img src ( http://localhost:3000/api/company/public/logo-1531296527722 ) 中使用它,它不會顯示。 如果我嘗試訪問此路由, Cannot GET /api/company/public/logo-1531296527722
收到此錯誤Cannot GET /api/company/public/logo-1531296527722
。 在所有相關問題中,nodejs 有自己的 html 處理器,如 ejs 或 jade,但我的情況是,nodejs 只是服務於我的前端,所有圖像都必須使用 vuejs 呈現。
我搞定了! 在我的 app.js 中,我包含app.use('/dist', express.static(path.join(__dirname + '/dist')));
我有同樣的問題。 更改您的代碼
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
對此:
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
這應該可以完成工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.