Node.js can´t create Blobs?

I am working with node.js and I streamed my Audio to my node.js server. Now I noticed during the process of building the audio blob:

var audioBlob = new Blob([dataview], { type: 'audio/wav' });

That I get a ReferenceError at new Blob. It seems that Blob is not supported. How can I create a blob which I would like to save with node.js fs module.

Thanks guys!



The Solution to this problem is to create a function which can convert between Array Buffers and Node Buffers. :)

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer

In recent node versions it's just:

let buffer = Buffer.from(arraybuffer);
let arraybuffer = Uint8Array.from(buffer).buffer;

Just use cross-blob:

const Blob = require("cross-blob");

new Blob([]);
//=> Blob {size: 0, type: ""}

// Global patch (to support external modules like is-blob).
globalThis.Blob = Blob;

As a suggestion, you might want to read this:

I mean I guess I don't know what you're trying to do. There may not be a module for blobs, but if you want to just write something to disk, there's the fs module.. This code won't work directly, but..

var fs = require('fs')
  , express = require('express')'/upload', function (req, res) {
  // asynch call to write file to disk
  fs.write("/tmp/file.mp3", req.params.body, function (err) {
    if (err) console.log(err)

Simply post an mp3, or anything really, to /upload, and it'll write it to disk. You can do whatever validation you want.


Another solution to consider is to use a Base64 String to transfer data from the Server to the client.

I am working on a Node.js project where I receive audio data in the form of an ArrayBuffer, and I want to send and play that data in the browser. Most of my struggles came from trying to send the ArrayBuffer to the client or trying to convert the ArrayBuffer and send a Buffer.

What ended up being a simple solution for me was to

  1. Convert the ArrayBuffer to a Base64 encoded String on the Server
  2. Return/Send the Base64 String to the client from the server
  3. Create an Audio element/object on the client side and play the sound

I used base64-arraybuffer to perform the ArrayBuffer > Base64 String conversion (though, it may be simple to do this without a package).

I used tips from here to create the audio element on the client side.

*I haven't done much in the way of testing limits - so I don't know how this might handle large audio files.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.