Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blob saved as [object Object] Nodejs

Tags:

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]

Here are some snippets of my code.

Frontend:

console.log(blob); $http.post('/api/save_recording', blob)   .success(function(new_recording) {     console.log("success");   }) 

The log prints:

Blob {type: "audio/wav", size: 237612, slice: function} success 

Backend:

exports.saveRecording = function(req, res) {   console.log(req.body);    fs.writeFile("temp/test.wav", req.body, function(err) {     if(err) {       console.log("err", err);     } else {       return res.json({'status': 'success'});     }   })  } 

The log prints: { type: 'audio/wav', size: 786476 }

Can you tell me why this isn't working, and how to fix it?

like image 882
Daniel Que Avatar asked Jun 02 '14 03:06

Daniel Que


People also ask

How do you declare a blob in node JS?

The "Blob is not defined" error occurs when the Blob class is used without being imported in a Node. js application. To solve the error import the Blob class before using it, e.g. import { Blob } from 'buffer'; . To solve the error, import the Blob class before using it.

What is blob in Nodejs?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

How do I save an object in node JS?

Storing your Node. js application's configuration data is quite simple - every object in JavaScript can be easily rendered as JSON, which in turn is just string data that can be sent or saved any way you'd like. The simplest way to do this involves the built-in JSON. parse() and JSON.

How do you convert an attachment to a blob?

If you must convert a file object to a blob object, you can create a new Blob object using the array buffer of the file. See the example below. const file = new File(['hello', ' ', 'world'], 'hello_world. txt', {type: 'text/plain'}); //or const file = document.


1 Answers

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'});     }   });  }; 
like image 104
Daniel Que Avatar answered Oct 06 '22 17:10

Daniel Que