Javascript 如何使用javascript删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2450433/
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
How to delete a file with javascript?
提问by T.T.T.
Did not have luck with these examples:
Javascript File remove
Javascript FSO DeleteFile Method
Deleting a File
这些例子没有运气:
Javascript File remove
Javascript FSO DeleteFile Method
Deleting a File
There are no special permissions on the file.
Is there a way to do this in JQuery?
该文件没有特殊权限。
有没有办法在JQuery 中做到这一点?
The requirement is - a certain file must be deleted from the web directory when another page is loaded. There is no security issue as this is on a closed network.
要求是 - 当加载另一个页面时,必须从 web 目录中删除某个文件。没有安全问题,因为这是在封闭网络上。
Any help is appreciated.
任何帮助表示赞赏。
Thanks.
谢谢。
回答by code_burgar
With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.
用纯 JavaScript 是做不到的。使用 AJAX 调用删除文件的服务器端脚本可以工作。
回答by Pekka
You can't delete files over HTTP (well in theory you can, but it's not implemented.)
你不能通过 HTTP 删除文件(理论上你可以,但它没有实现。)
The easiest way is to set up a tiny server side script (e.g. in ASP or PHP) and to call that from JavaScript. The server side script needs the proper permissions to do the deletion, but otherwise there is no problem.
最简单的方法是设置一个很小的服务器端脚本(例如在 ASP 或 PHP 中)并从 JavaScript 调用它。服务器端脚本需要适当的权限才能进行删除,否则没有问题。
In PHP the start would look like this: (Not expanding solution to a fully secure one because you're not saying what platform you are on)
在 PHP 中,开始看起来像这样:(不将解决方案扩展到完全安全的解决方案,因为您没有说明您在哪个平台上)
<?
// STILL INSECURE!!!!
// Do not use in any public place without authentication.
// Allows deletion of any file within /my/files
// Usage: filename.php?file=filename
$basedir = "/my/files";
$file_to_delete = $_REQUEST["file"];
$path = realpath($basedir."/".$file_to_delete);
if (substr($path, 0, strlen($basedir)) != $basedir)
die ("Access denied");
unlink($path);
?>
you would call the script like this:
你会像这样调用脚本:
http://yourserver/directory/delete_file.php?file=directory/filename
回答by Dustin Laine
Javascript cannot delete files, it is prevented as it would lead to HUGE security vulnerabilities. THose links are for ActiveX controls that are handled through JS. Use a server side language.
Javascript 不能删除文件,它被阻止,因为它会导致巨大的安全漏洞。这些链接用于通过 JS 处理的 ActiveX 控件。使用服务器端语言。
回答by Mike Daniels
You cannot delete a file on a remote server using only JavaScript running in a visitor's browser. This must be done with a server-side script.
您不能仅使用在访问者浏览器中运行的 JavaScript 来删除远程服务器上的文件。这必须使用服务器端脚本来完成。
回答by Quentin
If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.
如果您以 RESTFUL 方式执行此操作,您将发送HTTP DELETE 请求。
jQuery's ajax methodstates that you can use the method parameter to specify 'DELETE' but notes that some browsers may not support it.
jQuery 的ajax 方法声明您可以使用 method 参数来指定“DELETE”,但注意某些浏览器可能不支持它。
Obviously you will need a webserver which will accept a DELETE request, and apply some sort of authentication/authorization so that joe random visitor can't delete your files. I believe Apache's mod_davwill get you started here.
显然,您将需要一个接受 DELETE 请求的网络服务器,并应用某种身份验证/授权,以便 joe 随机访问者无法删除您的文件。我相信 Apache 的mod_dav会让你从这里开始。
回答by Anton
Javascript is a client side language. So you are not able to delete file on server directly. All examples that you provide may be used only for deleting files on your local machine but not into server.
Javascript 是一种客户端语言。所以你不能直接删除服务器上的文件。您提供的所有示例仅可用于删除本地计算机上的文件,而不能用于删除服务器中的文件。
But you may call some server page function that will delete file.
但是您可能会调用一些服务器页面函数来删除文件。
回答by Oscar Godson
You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.
您无法使用 JavaScript 删除文件,因为它在本地运行。因此,它甚至不涉及外部文件。
You need to use a server side language that has access to editing the files such as PHP, RoR, or ASP.
您需要使用有权编辑文件的服务器端语言,例如 PHP、RoR 或 ASP。
You can however use jQuery to call the server side code via AJAX such as $.get or $.post and then the server side code deletes it and it would seem as though JS is deleting the files.
但是,您可以使用 jQuery 通过 AJAX 调用服务器端代码,例如 $.get 或 $.post,然后服务器端代码将其删除,看起来好像 JS 正在删除文件。

