skip to Main Content

Is there a way we could set the size property of the File object. I will be getting the size as number from the api.

This is what I tried,

const file = new File([''], filename , { size: 100 })

This does not seem like working. Can someone help?

3

Answers


  1. The size is determined by the data in the File object. It isn’t an independent value that you can explicitly set.

    Login or Signup to reply.
  2. Have a look at File documentation

    const file = new File([new ArrayBuffer(100)], 'test.txt')
    
    console.log('The size of the file in bytes: ' + file.size + 'bytes')
    Login or Signup to reply.
  3. The problem with your code is your array buffer is empty so that you are getting 0 size. Also pass the second parameter as string for the filename.

     const file = new File(['hello world!'], 'filename')
     console.log(`size of file = ${file.size}`)

    As you are telling that you want to set the size of the file according to the received size then you have to put the data of that size in that file.

    As the third parameter is for type and last modified.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search