13

How can we create a file object or a blob object in Node js? The object required should be same as we get while uploading a file from a form in HTML.

5
  • Why would you want to? They're designed for the client environment and severely limited as a result. Commented May 15, 2015 at 6:19
  • 1
    I am trying to test a API which lets me upload a file but needs a file object or a blob object. I want to test is using node js but could not figure out a way to create a file or blob object Commented May 15, 2015 at 6:24
  • You're probably better off using a headless browser implementation like PhantomJS (if it has the File API, I don't know that that specific one does). Commented May 15, 2015 at 6:29
  • an uploaded file is just binary data, right? whatever object it was wrapped in is not relevant once the data is submitted from the browser to the server. On the server, assuming you're file is getting stored into a Buffer, well then, you can also fill a Buffer with binary data via fs.read or `fs.readSync' and use that Commented May 16, 2015 at 5:21
  • 1
    did you find any solutions dude? Commented Jul 14, 2019 at 17:15

1 Answer 1

3

You can't directly create objects from the File API, because that's an API designed for the client side and implemented by browsers.

If you wanted to, you could implement those same objects yourself (the API is fully documented, of course), but it would probably make more sense to use NodeJS's file system API instead.

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

1 Comment

Is the node js file api lets us create a file object? i mean i can access files i can do modfication and other stuff. But i specifically need to have a file object or a blob object as i want to test a api using Node js which takes in a file object as input

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.