Got a package reading and parsing files in browser
<input multiple accept=".abc" name="file" id="txtFileUpload" type="file" onchange="upload(event)"/>
function upload(event) {
var file = event.target.files[0];
somepackage.read(file, function(error, data) {...}
I need to use the same package in nodejs
fs.readFile("somefile.abc", 'utf8' , (err, file) => {
somepackage.read(file, function (error, data) {
// gives error
The response from web browser api and response from node js fs is not the same. Is it possible to convert the response from node's fs so it is the same as in the browser?
In the browser, the file
variable will be an instance of File
as described in the MDN page: https://developer.mozilla.org/en-US/docs/Web/API/File
Meaning it's a complete object with properties like name
, lastModified
…
Meanwhile in Node.js the value returned by fs.readFile
with the utf8
encoding will be the content of the file as a string.
So they're indeed incompatible. Depending on what somepackage.read
does with the File
object, you may be able to emulate a dummy object by retrieving date info with a separate fs.stat
call.
Fortunately, projects like https://github.com/node-file-api/file-api already took care of it.
Beware that if somepackage.read
assume that the provided file is a Blob
, you'll need to enhance the package mentioned above by using feature introduced very recently in Node.js 15.x : https://github.com/nodejs/node/pull/36811