我在客户端使用聚合物并作为图像上传按钮:
<vaadin-upload
form-data-name="file"
max-files="1"
id="fileUploadImage"
method="POST"
headers='{"Authorization": "bearer {{auth}}", "Content-Type": "multipart/form-data; boundary=XXXX"}'
target="{{config_endpoint}}/rest/upload_file/uploadFile"
nodrop
>
</vaadin-upload>
在服务器端,我使用 multer 上传图片:
const express = require("express");
const multer = require("multer");
var cors = require("cors");
const config = require("../../config");
const router = express.Router();
var corsOptions = {
origin: config.origin, // '*'
optionsSuccessStatus: 200
};
router.use(cors(corsOptions));
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "../../uploads");
},
filename: function(req, file, cb) {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, file.fieldname + "-" + uniqueSuffix);
}
});
var uploads = multer({ storage: storage });
router.post("/uploadFile", uploads.single("file"), (req, res) => {
console.log(req.files);
console.log(req.body);
});
module.exports = router;
对于 req.files 和 req.body 我都有未定义的值,请参阅日志:
13:54:44 0|eod | OPTIONS /rest/upload_file/uploadFile 200 0 - 0.553 ms
13:54:44 0|eod | undefined
13:54:44 0|eod | {}
我正在使用以下版本:“multer”:“^1.4.2”,nodejs v8.9.3
这是我的标题” 点击这里
怎么了 ?我错过了什么?顺便说一句,即使使用 Postman 我也遇到了同样的问题