I have this ajax:
$('.getid').on('click', function(){
var imagename = $(this).data("id");
// you can make ajax call here to get data
$.ajax({
type: "POST",
url: "Monitor/downloadDesiredImage",
data: {'val' : imagename},
dataType: "text"
}).done(function(data) {
console.log(data);
}).fail(function() {
alert( "error" );
});
});
This will work with a button that will send on.click a id value saved in a var imagename, that is a string that correspond to a image name saved on my server.
This is the php function that will get that id :
public function downloadDesiredImage() {
$imagename = $_POST['val'];
echo "this is my ". $imagename;
$file = file_get_contents('./images/'.$imagename.'.jpg', FILE_USE_INCLUDE_PATH);
$imageData = base64_encode(($file));
$src = 'data: '.mime_content_type($file).';base64,'.$imageData;
echo '<img src="' . $src . '">';
}
That will output the image in the network response, but I will also get the error :
Message: mime_content_type(): Invalid path
But what I want to do is after I click the button is to download that image from my server. How I can do that ? Thanks!