user4815896
user4815896

Reputation:

Error while using fs.readFileSync

I'm trying to convert an uploaded image to base 64

       var file = e.target.files[0];
       var imageFile = fs.readFileSync(file);
       var encoded = new Buffer(imageFile).toString('base64');

I get an error saying :

TypeError: __WEBPACK_IMPORTED_MODULE_4_fs___default.a.readFileSync is not a function.

Upvotes: 0

Views: 415

Answers (1)

Quentin
Quentin

Reputation: 943556

The mention of __WEBPACK_IMPORTED_MODULE_4_fs___ and the use of e.target.files suggests you are not running this code under Node.js but are bundling it up with WebPack and trying to run it in the browser.

The fs module requires Node.js.

You need to find a different approach if you want to do this in a browser.

See How to convert file to base64 in JavaScript?.

Upvotes: 1

Related Questions