我正在尝试构建一个API,它将采用图像这样使用的multer为此目的,我上传图像成功但它保存了一些奇怪的格式而不是我试图保存的实际格式。 我会在这里发布相关代码。
app.js中的
const multer = require('multer');
app.use(function(req, res, next) { //allow cross origin requests
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "http://localhost:3001");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Credentials", true);
next();
});
app.use(express.static('../client'));
app.use(bodyParser.json());
app.use(multer({dest:'./angular-src/src/assets/'}).single('file'));
app.use(express.static(path.join(__dirname,'public')));
在API文件中
const express = require('express');
const router = express.Router();
const multer = require('multer');
const storage = multer.diskStorage({ //multers disk storage settings
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const upload = multer({
storage: storage
}).single('file');
router.post('/upload',function (req,res) {
upload(req,res,function (err) {
if (err){
res.json({success:false});
return;
}else{
// console.log(req.files[0].originalname);
res.json({success: true, filename: req.file});
}
});
});
module.exports = router;
第二个中包含字母数字字符。
图像数据是这样的
{
"_type": "Project",
"_id": "AAAAAAFF+h6SjaM2Hec=",
"name": "Untitled",
"ownedElements": [
{
"_type": "UMLModel",
"_id": "AAAAAAFF+qBWK6M3Z8Y=",
"_parent": {
"$ref": "AAAAAAFF+h6SjaM2Hec="
},
"name": "Model",
"ownedElements": [
{
"_type": "UMLClassDiagram",
"_id": "AAAAAAFF+qBtyKM79qY=",
"_parent": {
"$ref": "AAAAAAFF+qBWK6M3Z8Y="
},
"name": "Main",
"visible": true,
"defaultDiagram": true
},
这就是我从邮递员那里打电话的方式
答案 0 :(得分:1)
对于那些有类似问题的人来说,这个问题已经解决了。对我有用的是
app.use(multer({dest:'./angular-src/src/assets/'}).single('file'));
在API类本身。