-1

I have one PHP file and other JavaScript. In JavaScript, I pass the variable with location of place where the file exists but on the PHP side doesn't recognize and doesn't properly download the file.

Code of JavaScript:

var nameFile = oEvent.getParameters().listItem.getTitle();
var directory = "C:/xampp/htdocsui5launchpad/WebContent/documents/";

window.location =directory;

$.ajax({
            url: 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php',
            type: 'POST',
            datatype: "json",
            data: { album: nameFile },
            success: function (response, data, xhr) {
                window.location = 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php';

            },
            error: function (response) {
                console.log("Error in PHP, downloadPDF.php ");
            }
        });

PHP code:

    if(isset($_POST["album"])){
        $name = $_POST["album"];
    }

    $dir = "C:/xampp/htdocs/ui5launchpad/WebContent/documents/";

    $file = $dir . $name;

    if( isset($file)) {
        //echo $file;

        if (file_exists($file)) {
            //echo $file;
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }

    }

Can you help me, please ?

Thanks

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
  • 1
    you can not download a file using AJAX, you need to call that URL directly into the browser. – Bhaumik Pandhi Jun 09 '17 at 14:48
  • Possible duplicate of [Download a file by jQuery.Ajax](https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) – Masivuye Cokile Jun 09 '17 at 14:51
  • 1
    Pandhi Bhaumik: I think you're wrong. Because if I have the url in php and call only php without parameters this work perfectly. My problem is when I pass the name – Pedro Santos Jun 09 '17 at 15:36
  • Maybe your SAPUI5 is doing GET rather than POST. Change `$_POST["album"]` to `$_REQUEST["album"]` or `$_GET["album"]` and see what happens. – developerwjk Jun 12 '17 at 16:10
  • Found it. Why after success of an ajax to a url are you redirecting to the same url? There's your problem. In the end `window.location = 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php';` is what is returning the file to where you can see it, and NOT the ajax call. But there's no parameter for the filename with your window.location code there. That's why its only working when you hardcode the filename. You can drop the ajax altogether and just do `window.location = 'http://localhost/ui5launchpad/WebContent/php/downloadPDF.php?album=' + nameFile;` – developerwjk Jun 12 '17 at 16:16
  • Not only that, but you had the totally unnecessary ajax code trying to download `datatype: "json",` when your php script its calling returns a PDF as `application/octet-stream`. – developerwjk Jun 12 '17 at 16:20
  • No one of this Suggestions works in my code. :( – Pedro Santos Jun 12 '17 at 16:54

1 Answers1

0

You don't need to use ajax to download a file. Just link the file direct to the browser.

In the link tag put the attribute like this:

<a href="http://example.com/file.pdf" download>Download</a>

Murilo Azevedo
  • 331
  • 1
  • 2
  • 7