如何发送数据正确 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">
post 工作正常,但通过 axios
不起作用.
post works correctly, but through axios
doesn't work.
请帮我改正错误
这是我的代码/--客户端
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
推荐答案
你可以这样做...
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
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()
})
}))
使用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 }));
希望我对你有用!:)
参考文献:
- github.com - 无法获得包含 Content-Type 的 .post...一个>
- github.com - 使用 axios、form-data、fs 的更好解决方案一个>
- https://stackoverflow.com/a/47630754/3332734
这篇关于如何发送数据正确 axios 错误:多部分:未找到边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文