Javascript - 复制文件

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

Javascript - copy file

javascripthtmlcopy

提问by Imran Abdur Rahim

This is the script I found via google. And I've edited the file name.

这是我通过谷歌找到的脚本。我已经编辑了文件名。

Here it is :

这里是 :

<html>
<head>File copy in progress...Don't close this page.</head>
<script type="text/javascript">  
    // initialize ActiveXObject and create an object of Scripting.FileSystemObject.  
    var fso = new ActiveXObject("Scripting.FileSystemObject");  

    // copies a file to another location + overwrites the file if already exits  
    fso.CopyFile("file1.txt", "file2.txt", 1);  


    fso = null;  
</script> 
</html>

But it is not copying anything? Actually is it possible in JavaScript to copy a file?

但它不是复制任何东西?实际上可以在 JavaScript 中复制文件吗?

回答by GitaarLAB

Turns out in the comments that the asker's goal is the folloing:

在评论中发现提问者的目标是以下内容:

Serve a html-page via http(s)
containing javascript that directly copy/modify files on the server that served the html-page.

通过
包含 javascript 的http(s) 提供 html 页面,这些 javascript 直接复制/修改提供 html 页面的服务器上的文件。

The answer my friend, is that as far as I'm aware you can't directly (using just client-side javascript from the browser) do that.

我的朋友的答案是,据我所知,您不能直接(仅使用浏览器中的客户端 javascript)这样做。

For starters, HTTP(S) is not a file-protocol like say.. ftp.

对于初学者来说,HTTP(S) 不是像 say.. ftp 那样的文件协议。

So, ultimately you'd either need to find a way to communicate with a network file protocol (for example ftp) with client-side javascript (I'm unaware of such a general lib, although you can do it in IE, but you'd need some developer-license registry-key's to access full network-features on windows, and you'd still need to do your own wrapping-library (or buy a specific activeX plugin, etc) to do the communication, apart from the rest of your interface).

所以,最终你要么需要找到一种方法来通过客户端 javascript 与网络文件协议(例如 ftp)进行通信(我不知道这样的通用库,虽然你可以在 IE 中做到这一点,但你需要一些开发人员许可注册表项来访问 Windows 上的完整网络功能,并且您仍然需要自己做包装库(或购买特定的 activeX 插件等)来进行通信,除了界面的其余部分)。

And the IE-specific code you posted in your answer is not meant to do this either (although it could access windows-shares you have access to).

而且您在答案中发布的特定于 IE 的代码也不打算这样做(尽管它可以访问您有权访问的 Windows 共享)。

So clearly, we can kind of forget about that option.

很明显,我们可以忘记那个选项。

So, since you say you already know how to do these things in php, your next step might be to add a javascript driven/enhanced (think tree-view etc) html-gui that sends commands (post or get) to your php-script that actually copies/modifies the files on your server. Ultimately you are then just recreating a php-file manager (google hint).

所以,既然你说你已经知道如何在 php 中做这些事情,你的下一步可能是添加一个 javascript 驱动/增强(想想树视图等)html-gui,它将命令(发布或获取)发送到你的 php-实际复制/修改服务器上文件的脚本。最终你只是重新创建一个 php 文件管理器(谷歌提示)。

If you'd go the node.js way, than this kind of just replaces php and the rest of the methodology is still the same: back-end executes commands and sends results/directory-lists etc. to HTML GUI (possibly via ajax).

如果您采用 node.js 方式,那么这种只是替换 php,其余方法仍然相同:后端执行命令并将结果/目录列表等发送到 HTML GUI(可能通过 ajax )。

You see, there are plenty of more fun things to explore, you are already half way there.

你看,还有很多有趣的事情要探索,你已经成功了一半。

Hope this helps.

希望这可以帮助。

EDIT:
One might cheat tough, you could download a file (and modify it) and store it in an HTML5 typed arrayand using http PUT (also supporting delete)storing it on the server again. Now you'd still need to handle security/user-identification... (and there php, json, some server-side handler still kicks-in once again, I'm currently not sure how apaches put-module works once enabled). Using server side includes that you have modified to display a directory-listing and some of this kind of javascript in the header might make a very crudefile-manager.. kind of.. Not exactly copying...

编辑:
一个人可能会作弊,你可以下载一个文件(并修改它)并将它存储在一个 HTML5类型的数组中,然后使用http PUT(也支持删除)再次将它存储在服务器上。现在你仍然需要处理安全/用户识别......(还有php, json ,一些服务器端处理程序仍然再次启动,我目前不确定一旦启用 apaches put-module 是如何工作的) . 使用服务器端包括您已修改以显示目录列表,并且标题中的某些此类 javascript 可能会使文件管理器变得非常粗糙......有点......不完全复制......

回答by Hogan

This is a strange example. First of all, it is not common for an HTML page to interact with the local file system. I would expect that any modern browser would complain ALOT about this code with warning and setttings requirements. In addition, it is using an activeX control which should only be available on IE if anywhere.

这是一个奇怪的例子。首先,HTML 页面与本地文件系统交互并不常见。我希望任何现代浏览器都会抱怨这个带有警告和设置要求的代码。此外,它使用的 ActiveX 控件应该只在 IE 上可用(如果在任何地方)。

Maybe this example is about 15 years old? That would explain a lot.

也许这个例子是大约 15 岁?那会解释很多。

I wouldn't expect this to work out of the box on any system installed / configured in the last 5 years.

我不希望这能在过去 5 年中安装/配置的任何系统上开箱即用。

You could change the settings on your system to let it work, but I would not recommend that. You system would be powned / zombied within minutes.

您可以更改系统上的设置以使其正常工作,但我不建议这样做。您的系统将在几分钟内被 pown/zombed。

I think we need to go back to what you are trying to do and why, I suspect that javascript in HTML is not the answer.

我认为我们需要回到您正在尝试做的事情以及为什么,我怀疑 HTML 中的 javascript 不是答案。



Based on the comment my suggestion is you work with node.js. You will learn a lot, have fun, and be working with a technology that is not 15 years out of date.

根据评论,我的建议是您使用 node.js。你会学到很多东西,玩得开心,并使用一种不过时 15 年的技术。

Go here, have fun:

来这里,玩得开心:

http://nodejs.org/

http://nodejs.org/

回答by CaldasGSM

rename your file extension to .hta and double click it.. voila..

将您的文件扩展名重命名为 .hta 并双击它..瞧..

http://en.wikipedia.org/wiki/HTML_Application

http://en.wikipedia.org/wiki/HTML_Application