Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is binary or ASCII with Node.js?

Tags:

I'm wondering what would be the best way to check if a file is binary or ASCII with Node.js?

There appears to be two ways not specific to node.js:

  1. Checking the MIME type: How to Check if File is ASCII or Binary in PHP - however this has it's problems, as for instance pre-precessors often don't have a recognised mime type and revert to application/octet-stream when checking them using mime

  2. Via checking the byte size using a stream buffer with How to identify the file content as ASCII or binary - which seems quite intensive, and does yet provide a node.js example.

So is there another way already? Perhaps a secret node.js call or module that I don't know about? Or if I have to do this myself, what way would be suggested?

Thanks

like image 498
balupton Avatar asked Apr 19 '12 09:04

balupton


1 Answers

Thanks to the comments on this question by David Schwartz, I created istextorbinary to solve this problem.

like image 150
balupton Avatar answered Sep 20 '22 20:09

balupton