3

I am trying to convert Blob to a File object in nodejs. The Blob is passed from my frontend to my backend. I then want to convert it in my backend.

Here is my current code:

frontend

var data = {
  "file": file, //this is blob
  "file_name": bucket_string
};

return await axios.post(`/files/upload-file`, data);

backend

const file = new File(data['file'], "tests/" + customer + "/" + sound + ".wav");

Currently this throws an error:

ReferenceError: File is not defined

How can I properly convert the data in nodejs?

Juliette
  • 4,309
  • 2
  • 14
  • 31
  • See the answer on this post: https://stackoverflow.com/a/31663645 – ErikMichelson Jan 12 '22 at 22:14
  • I attempted that above @ErikMichelson and got this: ReferenceError: File is not defined – Juliette Jan 12 '22 at 22:19
  • Nevermind, you're using node.js and not a browser. Node.js doesn't have the File class implemented. However, for working with files you have the [`fs`](https://nodejs.org/api/fs.html) module integrated in node. – ErikMichelson Jan 12 '22 at 22:24
  • 1
    @ErikMichelson so how can i use fs to convert blob to .wav File object – Juliette Jan 12 '22 at 23:01
  • What do you want to accomplish with the File object? If you're just interested in storing the wav file to disk, take a look at [`fs.writeFile`](https://nodejs.org/api/fs.html#fswritefilefile-data-options-callback). – ErikMichelson Jan 12 '22 at 23:50

1 Answers1

0

I don't have enough reputation to comment so sorry that this is a full post. File is an interface in TypeScript but does not specifically exist with node.js (sans TypeScript). I don't see anything referencing TypeScript in your code so maybe it's because you're not using typescript. For information on the File interface reference the docs here. Without knowing if you're using typescript I'm not sure what more to say about the File interface.

stuhops
  • 66
  • 6
  • 1
    I guess @Juliette isn't looking for the TypeScript File class, but for the [web implementation of File](https://developer.mozilla.org/en-US/docs/Web/API/File/File). However, that exists only for browsers. – ErikMichelson Jan 12 '22 at 22:32