Renaming a File() object in JavaScript

CBarr picture CBarr · Jun 9, 2015 · Viewed 22.4k times · Source

I'd like for my users to be able to re-name a file before uploading it.

I have a File object in Javascript which has a name property that is already set, but i'd like for this to be able to be updated. Right now doing the obvious myFile.name = "new-name.txt" returns an error that this property is read only.

What's the best way of changing the name property on a JavaScript File object?

Answer

PaulMest picture PaulMest · Apr 25, 2018

Now that file.name is a read-only property, I've found this to be the best method to rename a File object in the browser:

const myNewFile = new File([myFile], 'new_name.png', {type: myFile.type});