Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax调用从服务器文件夹下载图像_Php_Jquery_Ajax - Fatal编程技术网

Php 使用ajax调用从服务器文件夹下载图像

Php 使用ajax调用从服务器文件夹下载图像,php,jquery,ajax,Php,Jquery,Ajax,我有一个网站,大多数情况下都使用ajax。我允许用户通过ajax上传图像。当用户单击按钮时,图像通过ajax调用以模态显示 现在,我想让用户在不关闭模式或刷新的情况下单击图像开始下载。我确实试过用href。它工作得很好,但是,正如我提到的,我想让用户在同一个页面上打开模式 我一直尝试的代码是: $(document).ready(function(){ var imgname; imgname = ''; $("#modalimage").click(function()

我有一个网站,大多数情况下都使用ajax。我允许用户通过ajax上传图像。当用户单击按钮时,图像通过ajax调用以模态显示

现在,我想让用户在不关闭模式或刷新的情况下单击图像开始下载。我确实试过用href。它工作得很好,但是,正如我提到的,我想让用户在同一个页面上打开模式

我一直尝试的代码是:

$(document).ready(function(){
    var imgname;
    imgname = '';
    $("#modalimage").click(function(){
        imgname = $("#downloadimg").val();
        downloadImage(imgname);
    })
})
download.php代码是:

if ( isset($_POST['imagename']) ) {
    $filename = $_POST['imagename'];
    $filepath = 'images/'.$filename;
}
echo $filepath;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
这里的问题是,当ajax调用download.php时,它会以一些二进制代码和小图像代码的形式创建响应,而不启动下载。可以通过ajax调用下载图像吗


将此链接放置在以下位置,而不是通过ajax调用它:

<a href="download.php?imagename=<?php echo urldecode($imagename); ?>">
    Clich here to download
</a>
您不会被重定向到该文件,该文件将被下载。这不需要ajax

如果您确实更喜欢使用javascript,您可以通过友好方式创建链接:

$(document).ready(function(){
    $("#modalimage").click(function(){
        var imgname = $("#downloadimg").val();
        var link = document.createElement("a");
        link.download = name;
        link.href = 'download.php?imagename=' + encodeURI(imgname);
        link.click();
    });
})

据我所知,你想通过AJAX上传图像文件吗?我已经通过AJAX调用上传了图像。。现在我想通过ajax调用来下载图像好的,那么你希望你的用户能够通过ajax下载图像文件,对吗?请看下面的答案:我没有在这里显示重定向代码。。。我想通过制作ajax下载。。。我想我没有把我的问题说清楚我用了锚标签。。。但当我第一次使用t时,它会重定向到download.php,这是因为我忘记删除echo$filepath了吗。。?我会弄明白的。但是感谢您的帮助是的,回音是在发送任何头之前发出的,因为下载的头不在那里,所以url被重定向。没问题,我很高兴能帮上忙。
if ( isset($_GET['imagename']) ) {
    $filename = $_GET['imagename'];
    $filepath = 'images/'.$filename;
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
$(document).ready(function(){
    $("#modalimage").click(function(){
        var imgname = $("#downloadimg").val();
        var link = document.createElement("a");
        link.download = name;
        link.href = 'download.php?imagename=' + encodeURI(imgname);
        link.click();
    });
})