Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte size of buffer (Javascript / Node) [duplicate]

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!

like image 947
jaimefps Avatar asked Oct 26 '17 16:10

jaimefps


People also ask

What is buffer size in JavaScript?

For buffers containing UTF8 encoded strings, the buffer's length is equivalent to the length of the string. For example, if you read a text file from the file system using fs , the resulting buffer's length is the same as the number of characters in the text file.

How do you find the buffer size?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.

What is buffer data type in node JS?

What Are Buffers? The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.

How does the buffer store data in node JS?

A buffer is a space in memory (typically RAM) that stores binary data. In Node. js, we can access these spaces of memory with the built-in Buffer class. Buffers store a sequence of integers, similar to an array in JavaScript.

What is the length of a node node buffer?

Node.js buffers are objects that store arbitrary binary data. Buffers have a length property that contains the number of bytes in the buffer. const buf = Buffer.from ('Hello, World', 'utf8'); buf.length; // 12, same as 'Hello, World'.length For buffers containing UTF8 encoded strings, the buffer's length is equivalent to the length of the string.

What is buffer memory in Node JS?

A buffer is a temporary memory that a stream takes to hold some data until it is consumed. In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes. A buffer memory in Node by default works on String and Buffer.

What is the length of a string in a buffer?

Buffers have a length property that contains the number of bytes in the buffer. For buffers containing UTF8 encoded strings, the buffer's length is equivalent to the length of the string. For example, if you read a text file from the file system using fs, the resulting buffer's length is the same as the number of characters in the text file.

How is the size of a buffer determined in a stream?

In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes. A buffer memory in Node by default works on String and Buffer.


1 Answers

something like file.data.byteLength or file.data.toString().length will return the size in byte, divide with 1024 to get the size in kb

https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding

like image 167
Rahadian Kumang Avatar answered Oct 12 '22 02:10

Rahadian Kumang