Ajax call can’t get binary data
-
I got a function shows a picture. But when I try WordPress Ajax call to execute it, it returns <?php only, Is there any correct way to do it?
--PHP code
function get_image(){ $file='Large-Sample-Image-download-for-Testing.jpg'; //make it downloadable header("content-disposition: attachment; filename=test.jpg"); header('cache-control: must-revalidate'); header("expires: 0"); //return file contents readfile($file); }
--HTML page <htmL> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js" ></script> <script> jQuery(document).ready(function($) { $("#ajax_get_image_button").click(function() { var xhr = new XMLHttpRequest(); xhr.open('POST','get_image.php', true); xhr.responseType = "blob"; xhr.onload = function() { var img = document.createElement('img'); img.src=URL.createObjectURL(this.response); document.body.append(img); }; xhr.send(); }); }); </script> </head> <body> <input type="button" id="ajax_get_image_button" name="ajax_get_image_button" value="Ajax call to get image" /> </body> </html>
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘Ajax call can’t get binary data’ is closed to new replies.