T.T.T.
T.T.T.

Reputation: 34533

How to delete a file with javascript?

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.

Upvotes: 6

Views: 57338

Answers (7)

Oscar Godson
Oscar Godson

Reputation: 32726

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.

Upvotes: 1

Pekka
Pekka

Reputation: 449425

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

Upvotes: 3

Anton
Anton

Reputation: 9961

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.

Upvotes: 1

Quentin
Quentin

Reputation: 943556

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.

Upvotes: 1

Dustin Laine
Dustin Laine

Reputation: 38503

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.

Upvotes: 3

Mike Daniels
Mike Daniels

Reputation: 8642

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.

Upvotes: 2

code_burgar
code_burgar

Reputation: 12323

With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.

Upvotes: 13

Related Questions