如何使用 Javascript Apache 删除 excel 文件表单服务器

How can I delete an excel file form server with Javascript Apache

如何使用 Javascript Apache 删除一个 excel 文件表单服务器 我尝试了这段代码,但没有用。

var r = confirm("Are you sure you want to delete this File?")
        if(r == true)
        {
       $.ajax({
       url:'delete.php',
       data:'https://example/server/index.xlsx',
       method:'GET',
       success:function(response){
        if (response === 'deleted')
        {
           alert('Deleted !!');
        }
       }
      });
        }
  

delete.php

<?php 
   unlink($_GET['file']);
?>

下次您应该包括您收到的错误消息,以便帮助理解您的问题的人更容易理解。

试试这个

if(confirm("Are you sure you want to delete this file?")) {

    $.ajax({
        type: "POST",
        url: "delete.php", //assuming this file is in the same directory as the current file
        data: { filename: "/path/to/delete-file.xlsx" }, //this is the absolute path to your file you want deleted
        success: function(response) {
            if(response.success) {
                alert(response.message);
            }
        },
        error: function(xhr, opt, thrownError) {
            //alert(xhr.responseText);
            //alert(thrownError);
        }
    });
    
}

这是您的PHPdelete.php文件内容

<?php
    header('Content-type: application/json');
    
    $result = array(
        "success"   => 0,
        "message"   => "",
    );
    
    if(isset($_POST['filename'])) {
        if(file_exists($_POST['filename'])) {
            if(unlink($_POST['filename'])) {
                $result = array(
                    "success"   => 1,
                    "message"   => "File deleted successfully!",
                );
            }
        }
    }
    
    echo json_encode($result);
?>

首先,你不能用JS删除文件,因为它在本地运行。您需要使用服务器端文件来执行此操作。

delete.php

<?php
if(isset($_POST['path'])) {
    $path = $_POST['path'];

    //file exist?
    if(file_exist($path)) {
      //Remove file
      unlink($path);

     //Set status - deleted
     echo 'deleted';
  } else {
    echo 'not-deleted';
  } 
die;
}
die;

// Ajax 文件

    var r = confirm("Are you sure you want to delete this File?")
          if(r == true)
            {
var file_path = 'https://example/server/index.xlsx';
           $.ajax({
           url:'delete.php',
           type:'POST',
           data:{path: file_path},

           success:function(response){
            if (response === 'deleted')
            {
               alert('Deleted !!');
            }
           }
          });
            }