-1

i am this problem of not being able to download image from server to my PC(No saving prompt ) , no error message have been shown

The output i am receiving is some unreadable code from the google chrome inspect element

Thanks in advance

Javascript

function Download(id) {

            console.log(id);

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


 }

DownloadRequest.php File

<?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);
?>

Solution

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);

?>

Danvin Lee Qicheng
  • 57
  • 1
  • 2
  • 12
  • See this post for a download using jQuery http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery – Tristan Nov 21 '15 at 08:54

1 Answers1

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";
}
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41