PHP 文件下载有 ajax 个问题

PHP File Download with ajax Problems

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

我收到的输出是一些来自 google chrome 检查元素

的不可读代码

提前致谢

Javascript

function Download(id) {

            console.log(id);

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


 }

DownloadRequest.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";
}