我在PHP中创建一个下载页面,如果下载文件不存在(当用户点击相应产品的按钮时),将在Javascript中显示一个警告框。
问题是,一旦用户在警告框中点击“确定”,它就会导航离开当前页面(列出下载内容)。我希望它在同一页面上保持显示的相同内容。
完整代码:
<?php
$product = $_GET["p"];
$ChangeFileName = "";
if ($product == 'Product1')
{
$realFileName = "OriginalFileName.exe";
$ChangeFileName = "NewFileNameHere.exe";
}
elseif ($product == 'Product2')
{
$realFileName = "OriginalFileName.exe";
$ChangeFileName = "NewFileNameHere.exe";
}
else
{
echo '<script type="text/javascript">
alert("Sorry, there is no download available for this product! Please contact support.");
</script>';
exit;
}
$file = "downloads/".$realFileName;
$fp = fopen($file, 'rb');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$ChangeFileName");
header("Content-Length: " . filesize($file));
fpassthru($fp);
?>
exit;
答案 0 :(得分:0)
您可以在新窗口中打开下载页面;
var mutable = [1,2,3] // can change reference, add, remove, etc
let immutable = [4,5,6] // cannot replace reference or add, remove, etc
如果下载文件不存在警报并在返回第一页后关闭下载页面。
或者 Ajax下载: download file using an ajax request