How do I get the size (in bytes) of a bufferd image in javascript? I am not allowed to trust the file size in the client side, and need to verify in the backend as part of upload validation. My setup is as follows:
1- I upload a file in the client and send it to Node from a React component:
fileUpload() {
const url = '/.../...'
const formData = new FormData()
formData.append('file', this.state.file)
formData.append('actualSize', this.state.file.size) //not allowed to use this.
const config = { headers: { 'content-type': 'multipart/form-data' } }
axios.post(url, formData, config)
}
2- Using some middleware, I receive the data in Node as an object:
{
file: {
name: 'test',
data: <Buffer 12 50 4e ... >,
encoding: '7bit',
mimetype: 'image/png',
}
}
How can I measure the byte size of the buffer? Thanks in advance!