PHP文件下载与ajax问题


PHP File Download with ajax Problems

我遇到了无法将图像从服务器下载到我的电脑的问题(没有保存提示),没有显示错误消息

我收到的输出是来自谷歌chrome inspect元素的一些不可读代码

提前感谢

Javascript

function Download(id) {
            console.log(id);
             $.ajax({
              type: 'post',
              url: 'DownloadRequest.php',
              data: {filename: id.trim()},
            });

 }

下载请求.php文件

<?php
    $file = $_POST['filename'];
    header ("Content-Type: application/download");
    header ("Content-Disposition: attachment; filename=$file");
    header("Content-Length: " . filesize("$file"));
    $fp = fopen("$file", "r");
    fpassthru($fp);
?>

解决方案

function Download(id) {
    window.location="DownloadRequest.php?url="+id.trim();
 }
<?php
$file = $_GET['url'];;
header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$file");
header("Content-Length: " . filesize("$file"));
$fp = fopen("$file", "r");
fpassthru($fp);

?>

您可以这样尝试,而不是使用ajax、

window.location="DownloadRequest.php?filename";

最终代码,

function Download(id) {
   console.log(id);
   window.location="DownloadRequest.php?filename";
}