我想使用multer将录制的音频文件上传到Node js服务器,但是我没有在服务器上获取该文件。这是我的代码。该怎么办?
JS客户端代码:
mediaRecorder.onstop=(e)=>{
let blob=new Blob(chunks,{'type':'audio/mp3;'});
chunks=[];
let audioURL=window.URL.createObjectURL(blob);
audio.src=audioURL;
var data = new FormData();
var request = new XMLHttpRequest();
data.append('file',blob,'audio.mp3');
request.open('post','/upload');
request.send(data);
console.log('File sent');
}
服务器端代码:
app.post('/upload', upload.single('file'), function (req, res, next) {
console.log(req.file);
var oldpath = req.file;
var newpath = __dirname;
fs.readFile(oldpath, function (err, data) {
fs.writeFile(newpath, data, function (err) {
if (err) throw err;
res.end();
});
})
})
我现在收到此错误: See the output Here and Error