node.jsfile-uploadblobfsblobstore

upload a blob to server using fs


i have a site that records audio and returns a blob, goal is to send that blob from the client side vanilla javascript via fetch api or something, then send it to the node.js server then from there upload it to a certain directory using somehitng like fs.

function audio() {
    let blob = new Blob(chunks, { 'type': 'audio/mp3;' });
    // i want to send this blob to the back end server via fetch API
}

here is my backend

//----------Upload
spokenBio.post('/upload-spoken-bio', async (req, res) => {

    //waiting to get the blob or the file via req.body and somehow turn it into a real file write it 
    //into the server disk

})

Solution

  • To send, you can use the Fetch API:

    fetch('/upload-spoken-bio', {
      method: 'POST',
      body: blob
    });
    

    On the server, you can simply do something like:

    req.pipe(
      fs.createWritableStream('/some/path/file')
    );
    

    Of course, make sure you're trusting the data here, and don't let the user specify the path.