javascript 在javascript中重命名文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10618828/
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-26 10:28:15  来源:igfitidea点击:

rename a file in javascript

javascriptactivexfile-rename

提问by Eric

i want to ask that if i want to rename a file in javascript, what can i do ? i have try a function which i see it online but cannot work.

我想问一下,如果我想在 javascript 中重命名文件,我该怎么办?我尝试了一个我在网上看到但无法工作的功能。

function ChangeFileName()
{
    var fso, f;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.GetFile("FilePath/MyFile.txt");
    f.name = "MyFile.htm";
} 

i search online and it says that the ActiveXObject is only available for IE and i intended to use it on mozilla because mozilla comes with ubuntu.

我在网上搜索,它说 ActiveXObject 仅适用于 IE,我打算在 mozilla 上使用它,因为 mozilla 带有 ubuntu。

beside this, is there any method that i can rename a file inside the javascript ? thanks in advance for your help .

除此之外,有什么方法可以重命名javascript中的文件吗?在此先感谢您的帮助 。

采纳答案by Tiago Peczenyj

It is Javascript (in the browser), right?

它是 Javascript(在浏览器中),对吧?

If you run in the browser it is not allowed for security reasons. I think there is some way to do this using IE and ActiveX but using Pure Javascript I think it is not possible.

如果您在浏览器中运行,则出于安全原因是不允许的。我认为有一些方法可以使用 IE 和 ActiveX 来做到这一点,但使用纯 Javascript 我认为这是不可能的。

But you can do in JScript in the console, for example to delete a single file:

但是你可以在控制台的 JScript 中做,例如删除单个文件:

function MoveFile2Desktop(filespec)
{
   var fso;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   fso.MoveFile(filespec, "newname");
}

回答by Quentin

JavaScript has no built in means to interact with a file system.

JavaScript 没有与文件系统交互的内置方法。

A host object might provide such a means.

宿主对象可能提供这样的方法。

The host object (window) that is available to JavaScript loaded from a web page in a typical web browser exposes no such object. Web pages are not allowed to edit the disks of people visiting their sites. (The exception is IE, with ActiveX, and some security warnings).

window可用于从典型 Web 浏览器中的网页加载的 JavaScript的宿主对象 ( ) 不公开此类对象。网页不允许编辑访问其网站的人的磁盘。(IE、ActiveX 和一些安全警告除外)。

If you were running JavaScript in a browser extension or in a different environment (such as node.js) then it might be possible.

如果您在浏览器扩展或不同的环境(例如 node.js)中运行 JavaScript,那么它可能是可能的。

回答by Charles Boyung

No, you cannot rename a file with javascript. Javascript is not able to interact with the user's computer in any way - it is only intended to be used to interact with the content of the web page it is rendered on.

不,您不能使用 javascript 重命名文件。Javascript 无法以任何方式与用户的计算机进行交互 - 它仅用于与呈现它的网页的内容进行交互。