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?

5
  • See the answer on this post: stackoverflow.com/a/31663645 Commented Jan 12, 2022 at 22:14
  • I attempted that above @ErikMichelson and got this: ReferenceError: File is not defined Commented Jan 12, 2022 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 module integrated in node. Commented Jan 12, 2022 at 22:24
  • 1
    @ErikMichelson so how can i use fs to convert blob to .wav File object Commented Jan 12, 2022 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. Commented Jan 12, 2022 at 23:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess @Juliette isn't looking for the TypeScript File class, but for the web implementation of File. However, that exists only for browsers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.