Blob saved as [object Object] Nodejs

后端 未结 1 753
自闭症患者
自闭症患者 2020-12-13 20:00

I want to record audio from the microphone with HTML5, then send it to the server to be saved. Currently however, the saved file just contains [object Object]

相关标签:
1条回答
  • 2020-12-13 20:39

    I finally got this working. The approach to get this to work is to encode the blob on the client, and decode it on the server.

    Frontend:

    // converts blob to base64
    var blobToBase64 = function(blob, cb) {
      var reader = new FileReader();
      reader.onload = function() {
        var dataUrl = reader.result;
        var base64 = dataUrl.split(',')[1];
        cb(base64);
      };
      reader.readAsDataURL(blob);
    };
    
    blobToBase64(blob, function(base64){ // encode
      var update = {'blob': base64};
      $http.post('/api/save_recording', update)
        .success(function(new_recording) {
          console.log("success");
        });
    });    
    

    Backend:

    exports.saveRecording = function(req, res) {
      var buf = new Buffer(req.body.blob, 'base64'); // decode
      fs.writeFile("temp/test.wav", buf, function(err) {
        if(err) {
          console.log("err", err);
        } else {
          return res.json({'status': 'success'});
        }
      }); 
    };
    
    0 讨论(0)
提交回复
热议问题