Dynamically creating a blob from the computer's microphone. The blob is created without issue. I have tried to upload it to the backend and the files object is consistently empty
I have tried various forms of jquery ajax, vanilla javascript and fetch. On the backend I have tried to work with express-fileupload,multer and busboy.
//frontend function that creates the upload
upload.addEventListener("click", function(event){
var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
if (this.readyState === 4) {
console.log("Server returned: ", e.target.responseText);
}
};
console.log('blob is:',blob,' filename is:', filename);
let form = new FormData();
form.append('file-1', blob);
jQuery.ajax({
url: '/api/askjnj',
data: form,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: function(data){
alert(data);
}
});
})
//backend route code
const express = require('express')
const router = express.Router()
const db = require('../database/publicdata')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')
router.use(bodyParser.json())
router.use(
bodyParser.urlencoded({
extended: false,
})
)
router.use(fileUpload);
router.post('/askjnj',async(req,res)=>{
console.log('request files:',req.files);
console.log('req.body:', req.body);
res.send("ok");
})
module.exports = router
I expected the console to log a filename in the console log but instead I get null. Console log of blob on the client side shows the file was created.
update reworked the ajax call to test with the following
let obj =
'o' +
'\nv -0.500000 -0.500000 0.500000' +
'\nv 0.500000 -0.500000 0.500000' +
'\nv -0.500000 0.500000 0.500000' +
'\nvt 0.000000 0.000000' +
'\nvt 1.000000 0.000000' +
'\nvt 0.000000 1.000000' +
'\nvn 0.000000 0.000000 1.000000' +
'\nf 1/1/1 2/2/1 3/3/1';
blob = new Blob([obj]);
console.log(blob);
let formData = new FormData();
formData.append("files[]",blob, "test.wav");
formData.append("test","test");
$.ajax({
url:'/api/askjnj',
type: 'POST',
data: formData,
success: function (data) {
console.log(data);
},
error: function(err){
processAjaxErrors(err);
},
cache: false,
contentType: false,
processData: false
});
I am getting the test key in the req.body but req.files remains empty.