如何发送数据正确的axios错误:多部分:未找到边界 [英] How to send data correct axios Error: Multipart: Boundary not found
本文介绍了如何发送数据正确的axios错误:多部分:未找到边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道为什么我在服务器上收到 [错误:多部分:未找到边界]
和 bundle.js:37628 POST http:// localhost:8800 / exporttocsv 500(内部服务器错误)
当我通过
I don't know why I receive on server [Error: Multipart: Boundary not found] and bundle.js:37628 POST http://localhost:8800/exporttocsv 500 (Internal Server Error) When I make post through
<form action="/exporttocsv" method="POST" encType="multipart/form-data">
发布工作正常,但通过 axios
不行工作。
post works correctly, but through axios
doesn't work.
请帮我修复错误
Please help me fix the mistake
这个我的代码
/ - 客户端
this my code /--client
import axios from 'axios'
var formData = new FormData()
const config = { headers: { 'Content-Type': 'multipart/form-data' } };
export const ipmortToCSV = (file) => dispatch => {
formData.append('file',file)
console.log(formData.getAll('data'))
axios.post('/exporttocsv', {
"UploadCommand": formData
},config)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// - 服务器
const router = require('express').Router()
var csv = require('csv-express')
const controllers = require('../../controllers/exporttocsv')
var multer = require('multer')
var upload = multer({dest : 'exporttocsv/'})
router.get('/', (req, res) => {
controllers.exportToCsv(req,res)
})
router.post('/',upload.single('file'),(req,res) => {
//controllers.importToCsv(req,res)
})
module.exports = router
推荐答案
你可以这样做......
You can do this ...
const config = { headers: { 'Content-Type': 'multipart/form-data' } };
let fd = new FormData();
fd.append('file',files[0])
return axios.post("http://localhost:5000/upload", fd, config)
使用 concat
和 concat-stream
Usingconcat
and concat-stream
const concat = require("concat-stream")
const fd = new FormData()
fd.append("hello", "world")
fd.append("file", fs.createReadStream(file))
fd.pipe(concat(data => {
axios.post("/hello", data, {
headers: fd.getHeaders()
})
}))
使用承诺
Using promise
const promise = new Promise((resolve) => {
const fd = new FormData();
fd.append("hello", "world");
fd.append("file", fs.createReadStream(binaryFile));
fd.pipe(concat({ encoding: 'buffer' }, data => resolve({ data, headers: fd.getHeaders() })));
});
promise.then(({ data, headers }) => axios.post('/hello', data, { headers }));
我希望我一直很有用! :)
I hope I've been useful! :)
参考文献:
- github.com - 无法获取内容类型的.post ...
- github.com - 使用axios,form-data,fs的更好解决方案
- https://stackoverflow.com/a/47630754/3332734
- github.com - Can't get a .post with Content-Type...
- github.com - Better solution using axios, form-data, fs
- https://stackoverflow.com/a/47630754/3332734
这篇关于如何发送数据正确的axios错误:多部分:未找到边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文