In nodejs, I have:
const fileToUpload = fs.readFileSync(test_file_path);
console.log("fileToUpload: type: ", typeof fileToUpload, ", content: ", fileToUpload);
which prints:
type: object, content: <Buffer 50 4b 03 04 14 00 08 00 08 00 78 84 cb 50 00 00 00 00 00 00 00 00 24 ae 12 01 3e 00 20 00 31 2e 32 2e 38 34 30 2e 31 31 33 35 36 34 2e 31 30 2e 31 2e ... 10573784 more bytes>
I get that the type of fileToUpload is object. This is basic javascript type.
But what is <Buffer 50 4b 03 04 ....>? Is it binary format?
In frontend built with ReactJS, how can I convert an attached zip file to this <Buffer ... > type?
The reason I'm asking is that I need to get a file in such format in reactjs client side to be able to upload it to AWS s3 bucket. More details on: Javascript on client side: how to upload a zip file to AWS S3 bucket through pre-signed URL?
See Node.js docs:
https://nodejs.org/api/fs.html#fsreadfilesyncpath-options
If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.
So, to get a string you can write e.g.:
const text = fs.readFileSync(filePath, 'utf8');
The Buffer class is a subclass of JavaScript's Uint8Array.
The Buffer (Uint8Array) has some converter methods as well: