在 JavaScript 中重命名 File() 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30733904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:38:10  来源:igfitidea点击:

Renaming a File() object in JavaScript

javascriptfilerenamefile-rename

提问by CBarr

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

我希望我的用户能够在上传文件之前重新命名文件。

I have a Fileobject in Javascript which has a nameproperty 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.

File在 Javascript 中有一个对象,它有一个name已经设置的属性,但我希望能够更新它。现在做明显的myFile.name = "new-name.txt"返回一个错误,这个属性是只读的。

What's the best way of changing the nameproperty on a JavaScript Fileobject?

更改nameJavaScriptFile对象上的属性的最佳方法是什么?

采纳答案by beautifulcoder

You can add an inputtag with the name on it and hide the nameproperty from the user. On the server, just use the inputas the name and ignore the default name.

您可以添加input带有名称的标签name并对用户隐藏该属性。在服务器上,只需使用input作为名称并忽略默认名称。

回答by PaulMest

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

现在这file.name是一个只读属性,我发现这是在浏览器中重命名 File 对象的最佳方法:

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

回答by Alexander Taborda

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.

注意:这是针对图像类型的,您必须使用实际使用的类型更改此类型。

回答by Andrew Chart

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:

作为对 Alexander Taborda 的回答的回应...... Blob.slice() 的第一个和第二个参数是应该形成新 blob 的原始 blob 的开始和结束字节。通过说:

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".

您不是说“复制到文件末尾”(我认为这是您的目标),而是说“复制除最后一个字节之外的整个 blob”。

As @carestad says

正如@carestad 所说

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

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

然后创建一个新的 File() 对象应该创建一个具有新名称的精确副本。

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

请注意,对于 png,如果没有最后一个字节,文件将被视为无效。

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

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

回答by u10170457

You can use

您可以使用

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

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

它来自 MDN:https: //developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry/moveTo