I want to get local file as a file object no Buffer, I need File{} object. readSync giving me Buffer.
Asked
Active
Viewed 504 times
-2
-
Do you mean [this kind of File object](https://w3c.github.io/FileAPI/#file-section)? Which is designed for web applications? Not Node? – Quentin Mar 31 '22 at 14:04
-
1This reads like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You probably don't need a File object. What problem are you actually trying to solve? – Quentin Mar 31 '22 at 14:05
-
Local file on a client's computer, or local server file? Please add the code you've attempted to your question as a [mcve]. – Andy Mar 31 '22 at 14:05
-
Thanks for your response. I tried to mean the file of my project. Everytime a service generate json file, i want to read the file as a File object where all metadata includes – shrikanta mazumder Mar 31 '22 at 14:19
-
``` File { "clientName": "Shrikanta-mazumder.jpg", "extname": "jpg", "fileName": null, "fieldName": "profile_image", "tmpPath": "/tmp/ab-e9bfe920-fac7-491f-b4ff-917ac1d408b9.tmp", "headers": { "content-disposition": "form-data; name=\"profile_image\"; filename=\"Shrikanta-mazumder.jpg\"", "content-type": "image/jpeg" }, "size": 1050365, "type": "image", "subtype": "jpeg", "status": "consumed", "error": {} } ``` I want to read project file like this. – shrikanta mazumder Mar 31 '22 at 14:27
-
@shrikantamazumder — Well, that's not how readFile works. You'll need to get the various bits of data you are interested in through other APIs. – Quentin Mar 31 '22 at 14:34
1 Answers
-1
According to the documentation :
fs.readFile(path[, options])
options <Object> | <string>
encoding <string> | <null> Default: null
If no encoding is specified, then the raw buffer is returned.
Solution : specify an encoding.
fs.readFile(path, { encoding: "utf8"})

Jeremy Thille
- 26,047
- 12
- 43
- 63
-
No. this not return File object like sent from client. it returns Buffer or content or the file. I nee File object where all metadata of the file is included – shrikanta mazumder Mar 31 '22 at 14:15
-
"File object like sent from client"? What do you mean? You said you wanted to read a local file. – Jeremy Thille Mar 31 '22 at 14:18
-
i want to get like this ```File { "clientName": "Shrikanta-mazumder.jpg", "extname": "jpg", "fileName": null, "fieldName": "profile_image", "tmpPath": "/tmp/ab-e9bfe920-fac7-491f-b4ff-917ac1d408b9.tmp", "headers": { "content-disposition": "form-data; name=\"profile_image\"; filename=\"Shrikanta-mazumder.jpg\"", "content-type": "image/jpeg" }, "size": 1050365, "type": "image", "subtype": "jpeg", "status": "consumed", "error": {} }``` – shrikanta mazumder Mar 31 '22 at 14:23
-
"I want to get" from where? Who sends the file? From the front-end? With an ajax request? If so how do you read the file in the front-end? Your question is extremely vague and unclear. You need to provide a specific problem and explanation with the relevant code, not just "I want file object". – Jeremy Thille Apr 01 '22 at 10:01