Dev
Dev

Reputation: 1746

Cannot add value to file object size property

I created a file object in JavaScript and I'm not able to put value in the size property.

vm.allFiles[key] = new File([], value.fileName, {
    size: value.fileSize // this doesn't work
});
vm.allFiles[key].current = 'done';
vm.allFiles[key].fileID = value.id;
// vm.allFiles[key].size = value.fileSize - this gives error of Cannot assign to read only property 'size' of object '#<File>'

Edit: In regards to the answers.

The value returned from the server is just Object and not of File object kind. The reason why I need to have it as File object because new uploaded files will be stored to vm.allFiles array.

Upvotes: 1

Views: 1095

Answers (2)

kameron tanseli
kameron tanseli

Reputation: 109

A Blob object represents a file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Blob

Upvotes: -1

James Monger
James Monger

Reputation: 10665

The size property on File is inherited from Blob. The size property on Blob is read-only.

https://developer.mozilla.org/en-US/docs/Web/API/Blob

If you want to force the property to be overwritten, you can do this:

Object.defineProperty(vm.allFiles[key], 'size', {
    value: value.fileSize
});

Upvotes: 3

Related Questions