Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add an id property to a File?

I have an input to upload files. The uploaded files are shown as list displaying the file.name. When I add an id-property to a file it loses all it’s properties except the path property. How can I add an id properly?

const fileWithId = {...uploadedFile, id: "myIdName"}  //does not work

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can’t copy File objects, they’re largely-opaque data structures other than the specified properties, which don’t include an id.

You might be able to just assign an id property (since most objects in the browser environment are extensible, meaning you can add properties to them), but I wouldn’t recommend it. The definition of the File object changes over time, and you never know, it might get an id property that means something different from your id.

What you do instead will depend entirely on what you expect to do with the File. For instance, if this is just for client-side information, you might make your list a list of objects with id and file properties, where file is the original File object:

const fileWithId = {id: "myIdName", file: uploadedFile};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading