Renaming a File() object in JavaScript
Asked Answered
H

6

48

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?

Historicism answered 9/6, 2015 at 13:46 Comment(0)
P
5

You can add an input tag with the name on it and hide the name property from the user. On the server, just use the input as the name and ignore the default name.

Pearliepearline answered 9/6, 2015 at 14:1 Comment(3)
I already have a client side <input /> for the user to type the name into. Right now this actually might be the only feasible option as far as I can tell :( Still looking though...Historicism
Yeah,,, File objects are immutable. Doing this seems to be the best way, I could piece together a hack to extract the file data, turn it into a Blob with the new name, and then turn that into a File but going this route is much easier.Historicism
This doesn't really answer the question. You didn't say how to rename the file object or how to create copy of the object with different name. You basically said that OP should create workaround...Cosset
J
81

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});
Justus answered 24/4, 2018 at 22:56 Comment(5)
Clean, but File() constructor not supported at IE & Edge plus Opera for Android.Mordy
Thanks. Will it duplicate the memory? or just use the pointer to the existing blob referred by original file?Basle
@AkshayKhot were you able to find this out? I would also like to know if it's duplicated in memoryNotability
It doesn't look like it increased memory usage for me.Notability
@ChrisLang, yes, I think the new file just points to the existing blob, instead of duplicating it.Basle
D
18

try this:

var blob = file.slice(0, file.size, 'image/png'); 
var newFile = new File([blob], 'name.png', {type: 'image/png'});

note: this is for a image type, you have to change this type with type you're actually using.

Dillydally answered 20/9, 2017 at 17:8 Comment(1)
I would say, if you're working with a File object anyway, to use: var blob = file.slice(0, file.size, file.type)Groupie
P
5

You can add an input tag with the name on it and hide the name property from the user. On the server, just use the input as the name and ignore the default name.

Pearliepearline answered 9/6, 2015 at 14:1 Comment(3)
I already have a client side <input /> for the user to type the name into. Right now this actually might be the only feasible option as far as I can tell :( Still looking though...Historicism
Yeah,,, File objects are immutable. Doing this seems to be the best way, I could piece together a hack to extract the file data, turn it into a Blob with the new name, and then turn that into a File but going this route is much easier.Historicism
This doesn't really answer the question. You didn't say how to rename the file object or how to create copy of the object with different name. You basically said that OP should create workaround...Cosset
D
3

In response to Alexander Taborda's answer... The first and second parameters of Blob.slice() are the start and end bytes of the original blob that should form the new blob. By saying:

var blob = file.slice(0,-1);

you are not saying "copy to the end of the file" (which is what I think is your aim), you are saying "copy the whole blob except the last byte".

As @carestad says

var blob = file.slice(0, file.size);

followed by creating a new File() object should create an exact copy with a new name.

Note that with a png, the file will be considered invalid without the final byte.

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

Devout answered 22/3, 2018 at 20:51 Comment(0)
M
0

You can use

FileSystemEntry.moveTo(newParent[, newName][, successCallback][, errorCallback]);

to rename a file or directory.

It's from MDN : https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry/moveTo

Macula answered 1/11, 2019 at 6:52 Comment(1)
This is marked in MDN as both Experimental and Deprecated. Perhaps this is why this answer got no votes in a couple of years. Since JavaScript is insecure, it should not have direct access to the client file system, although there are plenty of loopholes, unfortunately.Chrysalid
K
0

Assuming f is original file object, just use spread operator, example of regexing the name:

let newF = {
        ...f,
        name: f.name.toString().replace(/[^0-9A-Za-z.\-_()]/gi, '')
      };
Keelson answered 18/11, 2021 at 16:8 Comment(1)
Unfortunately this doesn't work since f.name is read only. :(Madrigal

© 2022 - 2024 — McMap. All rights reserved.