Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
85
rated 0 times [  92] [ 7]  / answers: 1 / hits: 54766  / 6 Years ago, fri, march 30, 2018, 12:00:00

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 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);
});


//--server



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

More From » node.js

 Answers
4

You can do this ...



Instantiate a new FormData instance.



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)


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! :)



References:




[#54813] Tuesday, March 27, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lamarmaximiliand

Total Points: 388
Total Questions: 104
Total Answers: 104

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;