Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone File objects in javascript?

I thought I could clone File object in the same way as any other object with: JSON.parse(JSON.stringify(file))

But, JSON.stringify(file) always evaluates to "{}". Is there any workaround besides creating empty object and iterating over all properties of file object and adding them to it?

Also is there any reason why File objects cannot be converted to string? I though they only store information about file on disk, such as name and path, not the file contents.

like image 305
displayName Avatar asked Jul 29 '18 09:07

displayName


3 Answers

new File([blob], blob.name, { type: blob.type });

Adding the type is important. The accepted answer missed the type.

like image 103
MrSc Avatar answered Sep 20 '22 22:09

MrSc


new File([file], file.name) returns copy of file object

like image 36
displayName Avatar answered Sep 20 '22 22:09

displayName


hjfgfg

like image 33
Anonymous Avatar answered Sep 19 '22 22:09

Anonymous