javascript 如何使用javascript从文件夹中删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19721621/
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 file from folder using javascript?
提问by Simranjeet Kaur
Is there any way to delete files from folder using javascript..? Here is my function
有没有办法使用javascript从文件夹中删除文件..?这是我的功能
function deleteImage(file_name)
{
var r = confirm("Are you sure you want to delete this Image?")
if(r == true)
{
var file_path = <?php echo dirname(__FILE__) . '/uploads/'?>+file_name;
file_path.remove();
}
}
回答by zzlalani
You cannot delete anything without any server-side script..
如果没有任何服务器端脚本,您将无法删除任何内容。
You can actually use ajax and call a server-side file to do that for e.g.
您实际上可以使用 ajax 并调用服务器端文件来执行此操作,例如
Make a file delete.php
制作文件delete.php
<?php
unlink($_GET['file']);
?>
and in the javascript
并在 javascript 中
function deleteImage(file_name)
{
var r = confirm("Are you sure you want to delete this Image?")
if(r == true)
{
$.ajax({
url: 'delete.php',
data: {'file' : "<?php echo dirname(__FILE__) . '/uploads/'?>" + file_name },
success: function (response) {
// do something
},
error: function () {
// do something
}
});
}
}
回答by Kuldeep
You can not delete files with javascript for security reasons.However, you can do so with the combination of server-side language such as PHP, ASP.NET, etc using Ajax. Below is sample ajax call that you can add in your code.
出于安全原因,您不能使用 javascript 删除文件。但是,您可以结合使用 Ajax 的服务器端语言(例如 PHP、ASP.NET 等)来删除文件。下面是您可以在代码中添加的示例 ajax 调用。
$(function(){
$('a.delete').click(function(){
$.ajax({
url:'delete.php',
data:'id/name here',
method:'GET',
success:function(response){
if (response === 'deleted')
{
alert('Deleted !!');
}
}
});
});
});
回答by Cody Donelson
Using NodeJS you can use filestream to unlink the file also.
使用 NodeJS,您还可以使用文件流来取消链接文件。
It'd look something like this:
它看起来像这样:
var fs = require('fs');
fs.unlink('path_to_your_file+extension', function (err) {
//Do whatever else you need to do here
});
I'm fairly certain (although I havent tried it) that you can import the fs node_module in plain javascript but you'd have to double check.
我相当确定(尽管我还没有尝试过)您可以在纯 javascript 中导入 fs node_module,但您必须仔细检查。
If you cant import the module you can always download NPM on your machine, and
如果您无法导入模块,您可以随时在您的机器上下载 NPM,并且
npm i fs
in some directory (command line) to get the javascript classes from that module to use on your markup page.
在某个目录(命令行)中从该模块获取 javascript 类以在您的标记页面上使用。
回答by nrsharma
You can't do this. Actually JavaScript is sandboxed
and it's not allowing to do such operations.
你不能这样做。实际上JavaScript is sandboxed
,它不允许进行此类操作。
For deleting a file you need a server side scripting to achieve this. It depends on the fact that what server side language you are using to deal with.
要删除文件,您需要一个服务器端脚本来实现这一点。这取决于您使用哪种服务器端语言来处理这一事实。
回答by Pranav Kale
Javascript is a client side scripting language. If you want to delete files from server, use php instead.
Javascript 是一种客户端脚本语言。如果要从服务器删除文件,请改用 php。
回答by Dilantha
You cannot do it by using javascript. But if the file resides in the server then you can use php to do that..you can use unlink in php.
你不能通过使用 javascript 来做到这一点。但是如果文件驻留在服务器中,那么你可以使用 php 来做到这一点..你可以在 php 中使用 unlink。
unlink($path_to_file);