如何使用 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 !!');
}
}
});
}
洗掉.php
<?php
unlink($_GET['file']);
?>
uj5u.com熱心網友回復:
下次您應該包括您收到的錯誤訊息,以便幫助理解您的問題的人更容易。
嘗試這個
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);
}
});
}
這是你的 PHP delete.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);
?>
uj5u.com熱心網友回復:
首先,你不能用JS洗掉檔案,因為它是在本地運行的。您需要使用服務器端檔案來做到這一點。
洗掉.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 !!');
}
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317900.html
標籤:javascript 阿帕奇 文件 服务器
