How to delete a file using javascript?

No luck with these examples:
Javascript file to delete
Javascript FSO Removal Method
File deletion

There are no special permissions in the file.
Is there a way to do this in jQuery ?

Requirement - a specific file must be deleted from the web directory when loading another page. There is no security issue because it is in a closed network.

Any help is appreciated.

Thanks.

+4
source share
7 answers

With pure JavaScript, this is not possible. Using an AJAX call on the server side script that will delete the file will work.

+10
source

You cannot delete files via HTTP (well, theoretically you can, but this is not implemented.)

The easiest way is to configure the tiny server side of the script (e.g. in ASP or PHP) and call it from JavaScript. The server side of the script needs proper permissions to delete, but otherwise there is no problem.

In PHP, the beginning will look like this: (Without extending the solution to be completely secure, because you are 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 call the script like this:

 http://yourserver/directory/delete_file.php?file=directory/filename 
+3
source

Javascript cannot delete files, this is prevented, as this will lead to a HUGE security vulnerability. These links are for ActiveX controls that are processed through JS. Use the server language.

+3
source

You cannot delete a file on a remote server using only JavaScript running in the visitors browser. This should be done on the server side of the script.

+2
source

If you do this using RESTFUL, you will send an HTTP DELETE request .

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

Obviously, you will need a web server that will accept a DELETE request and apply some authentication / authorization so that a random visitor joe cannot delete your files. I believe Apache mod_dav will help you get started here.

+1
source

Javascript is a client language. Thus, you cannot delete the file on the server directly. All the examples you provided can only be used to delete files on the local computer, but not on the server.

But you can call the server page function, which will delete the file.

+1
source

You cannot delete files using JavaScript because it runs locally. Thus, it does not even concern external files.

You need to use a server-side language that has access to edit files such as PHP, RoR or ASP.

However, you can use jQuery to call server-side code via AJAX, such as $ .get or $ .post, and then server-side code removes it, and it looks like JS is deleting files.

+1
source

Source: https://habr.com/ru/post/1304201/


All Articles