如何在javascript中获得缓冲图像的大小(以字节为单位)?我不允许在客户端信任文件大小,需要在后端进行验证,作为上传验证的一部分。我的设置如下:
1-我在客户端上传一个文件,并将其从React组件发送到节点:
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-使用一些中间件,我将节点中的数据作为对象接收:
{
file: {
name: 'test',
data: <Buffer 12 50 4e ... >,
encoding: '7bit',
mimetype: 'image/png',
}
}
如何测量缓冲区的字节大小?提前感谢!