I am working on a code to compress files using PHP and download the zip, How do I initiate the download using jQuery?
When I reload the PHP script through the browser, the file is downloaded normally as zip, but when I send a request from jQuery, the file is returned from the server as gibberish characters when I visualize in the browser console. Please not I can automatically download a file using php, but the problem is when I try to use jQuery to fetch the zip file
The php code in the file file.php
$file_path = '/path/tofile/file.php'
$file_name = basename($file_path);
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Length: ".filesize($file_path));
flush();
readfile($file_path);
The corresponding ajax request
$.get('file.php', function(data)
{
}
);
I expect the file to be downloaded as a normal zip file from jQuery but instead gibberish characters are sent to the browser