0

I need to use javascript on my server application. I have a variable holding a path to a PDF.

Now I want to read that file as a byte array to send it to another server. In Java, I would do something like this:

String myPath = "\path\to\my.pdf";

try {
  byte [] myFile = Files.readAllBytes(Paths.get(myPath));

  //Own function
  sendData(myFile);
} catch (IO Exception e) {
  e.printStackTrace();
} 

I don't know what the javascript equivalent would be to this. With the fs library I can only read a file as an object of type 'Buffer':

import fs from 'fs';
let myPath = '\path\to\my.pdf';
let myFileAsBuffer = fs.readFileSync(myPath, { flag: 'r' });
//Own function
sendData(myFileAsBuffer);

But I always get an error because the server expects a byte array and not a Buffer, which looks something like this:

<Buffer 25 50 44 46 2d 31 2e 35 0d 0a 25 b5 b5 b5 b5 0d 0a 31 20 30 20 6f 62 6a 0d 0a 3c 3c 2f 54 79 70 ...>
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30
schande
  • 576
  • 12
  • 27
  • Does this answer your question? [How to read a local text file?](https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file) – f1sh Jul 12 '21 at 13:46
  • 1
    @f1sh I don't see how it applies here. OP seems to want to read it from the disk, not as a web request to the local machine. And wants a byte array. – VLAZ Jul 12 '21 at 13:47
  • 1
    Have you tried: `[...myFileAsBuffer.values()]`? See: [`buf.values()`](https://nodejs.org/api/buffer.html#buffer_buf_values) – Mr. Polywhirl Jul 12 '21 at 13:50
  • 1
    I did not know, that you could use the spread syntax like this. Thanks for your help! – schande Jul 12 '21 at 14:39

2 Answers2

2

You should spread the iterable values() of the Buffer before calling sendData:

sendData([...myFileAsBuffer.values()])

See: buf.values()


Here is an equivalent example to Java:

import fs from 'fs';

class Files {
  static readAllBytes(path) {
    return [...fs.readFileSync(path, { flag: 'r' }).values()];
  }
}

sendData(Files.readAllBytes('/path/to/my.pdf'));

Note that the path for the fs.readFileSync function can take various types i.e. string, Buffer, URL, or integer. The static function in the example above is assuming that your path is one of those four types.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Just one important note to the other answer given:

In Node.js you should stay away from functions like readFileSync. As you know Node.js is single-threaded. And the moment you call a function like readFileSync you're locking up your entire application until the data is loaded from disk. (like a short cardiac arrest)

So, the better solution is to use readFile which can be used with a callback function or promise.

The only few times when readFileSync is acceptable, is when the application is still initializing, e.g. to load settings. For that reason, if you're making a client/server application, I would definitely stay away from readFileSync.

import { readFile } from 'node:fs/promises';

// use in an async function:  
const contents = await readFile(filePath);
bvdb
  • 22,839
  • 10
  • 110
  • 123