I'm trying to pass a blob of audio file from my react
application to Express
backend using formData, but I kept on getting undefined when I try to retrieve the data on the backend.
Frontend code:
var fd = new FormData();
fd.append('fname', 'test.webm');
fd.append('data', blob);
payload = {audioFile: fd};
axios.post('/translate-audio', payload, {headers: {'content-type': 'multipart/form-data'}})
backend code:
app.use(cors());
app.use(express.json());
app.post('/translate-audio', async (req, res) => {
console.log(req.body.audioFile);
});
Side note:
I was able to see the file when I ran console.log(payload.audioFile.get('data'));
on the frontend:
File {name: 'blob', lastModified: 1636600689885, lastModifiedDate: Wed Nov 10 2021 19:18:09 GMT-0800 (Pacific Standard Time), webkitRelativePath: '', size: 10828, …}
lastModified: 1636600689885
lastModifiedDate: Wed Nov 10 2021 19:18:09 GMT-0800 (Pacific Standard Time) {}
name: "blob"
size: 10828
type: "audio/webm;codecs=opus"
webkitRelativePath: ""
Any help is greatly appreciated. I just need to pass the blob correctly to the backend, so other alternative without using formData will be helpful as well. Also, what is a good way to do this the other way around (sending audio blob from server to frontend)?