我已经在我的node js
文件中创建了一个axios调用,它正在获得正确的图像,但当它被发送到api时,没有文件&内容类型比实际尺寸小得多。
我发送文件的方式:
axios.post('http://api.site.com',
req.files.files,
{
headers:
{
'Content-Type': 'multipart/form-data',
}
} )
.then(function(result) {
console.log(result.data);
}) .catch(function(){
console.log('FAILURE');
});
return res.json({"status":"success","message":"success"})
点播。文件控制台日志:
{
files: {
name: 'Group4.png',
data: <Buffer >,
size: 203091,
encoding: '7bit',
tempFilePath: '/tmp/tmp-1-1658204721135',
truncated: false,
mimetype: 'image/png',
md5: '5dd1c4edd7ca59c8e05550ebace12fd8',
mv: [Function: mv]
}
}
发送给api的报头:
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'multipart/form-data',
'User-Agent': 'axios/0.21.4',
'Content-Length': '212',
我也在使用Cors,身体解析器,express-file-upload和express插件。如果有人能帮我,那就太好了。
您可以尝试使用multer。只需用下面的代码尝试一次。请参考此链接https://www.section.io/engineering-education/uploading-files-using-multer-nodejs/
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
const upload = multer({storage: storage})
router.get('/', function(req, res, next) {
// get your file inside /uploads
});
router.post('/profile', upload.single('avatar'), function (req, res, next) {
// write your rest of the code like save it on db etc.
})