0

I have a problem with importing any file as a file instance from local folder. Can you help me please? I can't find anything in 'fs' or 'path', maby because I don't know what to see I want to get File and pass it to my JS applicatin as a File instance.

  • Please provide a concrete example of what you are trying to achieve and how you tried it. What do you mean by "file instance"? This terminology is not used in Node. – Felix Kling Dec 05 '18 at 00:22
  • Please post your code. it is not possible to understand based on the info provided – SanSolo Dec 05 '18 at 03:33

1 Answers1

0

Start by using a path relative to your script and the __dirname directive.

fs.readFileSync(__dirname + '/myfile.txt')

You may also wish to lose the / using path to make your code more portable.

const path = require('path')
fs.readFileSync(path.join(__dirname, 'myfile.txt')

In regards to format, the result will be Buffer. If you want a string,

fs.readFileSync(filename,'UTF-8')

If you want a blob, see stackoverflow.com/questions/14653349

Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • Thank you but when I will send this to my js application how can I retrieve File instance from this? – Danylo Katran Dec 05 '18 at 09:06
  • Will this be a Buffer and I can just do tike this: `const file = new Blob( [response], {type: some.mimeType} );` – Danylo Katran Dec 05 '18 at 09:11
  • It will be buffer. If you want string, `fs.readFileSync(filename,'UTF-8')`. If you want blob, see https://stackoverflow.com/questions/14653349/node-js-can%C2%B4t-create-blobs – Steven Spungin Dec 05 '18 at 09:49