I'm using image picker plugin to select an image then force it to download. I tried hard coding the PHP and it works as expected. Download pops-up and I managed to view the file. I'm not sure why it didnt work in AJAX. What am I missing?
//get the image-source or URL
function getImageSrc()
{
var req = $("div[class='thumbnail selected']").children('img');
var imagessource = [];
$(req).each(function (datakey, datavalue) {
src = $(datavalue).attr('src');
imagessource.push(src);
});
return(imagessource);
}
Here's when I click the button
$("#download_media").click(function() {
var file = getImageSrc();
$.ajax({
type: 'POST',
url: '?page=downloadController&action=downloadMedia',
data: {'file_url': file},
success: function(data) {
console.log("success");
}
});
});
My PHP FILE
public function downloadMediaAction()
{
//get the file_url
$file = $this->getRequest()->('file_url');
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
}