带有ajax问题的PHP文件下载

时间:2015-11-21 08:49:52

标签: javascript php jquery ajax upload

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

我收到的输出是来自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);

&GT;

1 个答案:

答案 0 :(得分:1)

you can try like this, instead of using ajax,

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

Final code,

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