最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to delete a file with javascript? - Stack Overflow

programmeradmin1浏览0评论

Did not have luck with these examples:
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?

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.

Any help is appreciated.

Thanks.

Did not have luck with these examples:
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?

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.

Any help is appreciated.

Thanks.

Share Improve this question edited Mar 15, 2010 at 21:34 sepehr 18.5k7 gold badges86 silver badges120 bronze badges asked Mar 15, 2010 at 20:58 T.T.T.T.T.T. 34.5k47 gold badges134 silver badges172 bronze badges 2
  • 2 ActiveX works only on IE browsers. – Buhake Sindi Commented Mar 15, 2010 at 21:01
  • [visit this link for help :) here someone else has answered this already using jquery and ajax..][1] [1]: stackoverflow.com/questions/2665832/… – saadk Commented Jun 6, 2013 at 8:54
Add a comment  | 

7 Answers 7

Reset to default 13

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

You can't delete files over HTTP (well in theory you can, but it's not implemented.)

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.

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)

<? 

  // 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

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.

If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.

jQuery's ajax method states that you can use the method parameter to specify 'DELETE' but notes that some browsers may not support it.

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_dav will get you started here.

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.

But you may call some server page function that will delete file.

You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.

You need to use a server side language that has access to editing the files such as PHP, RoR, or 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.

发布评论

评论列表(0)

  1. 暂无评论