【发布时间】:2019-05-23 10:42:25
【问题描述】:
我想使用 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
【问题讨论】:
-
@iambatman 谢谢,但我试过了,它不起作用
-
你应该在xmlhttp请求中设置内容类型头
-
这里是`request.setRequestHeader("Content-Type", "multipart/form-data");`
标签: javascript node.js file-upload multer mediarecorder-api