0

I want to create xlsx file and send to the browser as an attachment, so the user downloads it immediately. I literally did copy-paste the code from this example. But it doesn't work. I fixed the path in require_once, but the issue is somewhere else.

The xlsx file is generated corerctly - when I save ot on the server, I can open it. It is also sent to the browser - firebug's console shows some funny characters in output window. The headers are also correct.

But no Save as... dialog is shown. I did some basic checks based on google search results - I have no extra white space after ?>.

The only difference in my code is that I call php script from jQuery's $.post function with some additional arguments.

Could it be the reason why I can't download this file?

Any help would be greatly appreciated!

  • PHP version: 5.4.20
  • PHPExcel version: 1.8.0
  • Server: Apache/2.4.6 (Linux/SUSE)

This question is also posted on codeplex.

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
maialithar
  • 3,065
  • 5
  • 27
  • 44

2 Answers2

1

You can't download files via an ajax request such as $.post for security reasons.

You could use a link that opens in a new window instead.

MajorCaiger
  • 1,893
  • 1
  • 12
  • 18
  • Thanks for guiding me, I found a solution [here](http://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data). – maialithar Jul 11 '14 at 10:27
0
<?php
// If user click the download link
if(isset($_GET['filename'])){
        // The directory of downloadable files
        // This directory should be unaccessible from web
        $file_dir="";


// Replace the slash and backslash character with empty string
        // The slash and backslash character can be dangerous
        $file_name=str_replace("/", "", $_GET['filename']);
        $file_name=str_replace("\\", "", $file_name);


// If the requested file is exist
        if(file_exists($file_dir.$file_name)){
                // Get the file size
                $file_size=filesize($file_dir.$file_name);
                // Open the file
                $fh=fopen($file_dir.$file_name, "r");


// Download speed in KB/s
                $speed=50;



// Initialize the range of bytes to be transferred
                $start=0;
                $end=$file_size-1;


// Check HTTP_RANGE variable
                if(isset($_SERVER['HTTP_RANGE']) &&
                        preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER['HTTP_RANGE'], $arr)){

                        // Starting byte
                        $start=$arr[1];
                        if($arr[2]){
                                // Ending byte
                                $end=$arr[2];
                        }
                }


// Check if starting and ending byte is valid
                if($start>$end || $start>=$file_size){
                        header("HTTP/1.1 416 Requested Range Not Satisfiable");
                        header("Content-Length: 0");
                }
                else{
                        // For the first time download
                        if($start==0 && $end==$file_size){
                                // Send HTTP OK header
                                header("HTTP/1.1 200 OK");
                        } 
                        else{
                                // For resume download
                                // Send Partial Content header
                                header("HTTP/1.1 206 Partial Content");
                                // Send Content-Range header
                                header("Content-Range: bytes ".$start."-".$end."/".$file_size);
                        }


// Bytes left
                        $left=$end-$start+1;


// Send the other headers
                        header("Content-Type: application/octet-stream");
                        header("Accept-Ranges: bytes");
                        // Content length should be the bytes left
                        header("Content-Length: ".$left);
                        header("Content-Disposition: attachment; filename=".$file_name);

                        // Read file from the given starting bytes
                        fseek($fh, $start);
                        // Loop while there are bytes left
                        while($left>0){
                                // Bytes to be transferred
                                // according to the defined speed
                                $bytes=$speed*1024;
                                // Read file per size
                                echo fread($fh, $bytes);
                                // Flush the content to client
                                flush();
                                // Substract bytes left with the tranferred bytes
                                $left-=$bytes;
                                // Delay for 1 second
                                sleep(1);
                        }
                }


fclose($fh);
        }
        else
        {
                // If the requested file is not exist
                // Display error message
                echo "File not found!";

        }

        exit();
}
?>

<a href="download.php?filename=filename.pdf">Download.</a>

You can use this code for download you can edit extension and also speed

Gökhan Çokkeçeci
  • 1,388
  • 3
  • 16
  • 37