WordPress Ajax 调用无法获取二进制数据,仅返回 <?php

WordPress Ajax call can't get binary data, it returns <?php only

提问人:pang-db-builder 提问时间:11/6/2023 最后编辑:pang-db-builder 更新时间:11/8/2023 访问量:34

问:

好吧,我可以在没有 WordPress 的情况下让 Ajax 调用返回二进制数据,例如图像。但我就是无法让它在 WordPress 中工作。它返回 <?php 而不返回其他任何内容。在我通读WordPress ajax-admin.php源代码以找出那里发生了什么之前,我想我最好在这里询问一下,看看我是否遇到了已知问题以及这里已经存在的任何解决方法。

我的工作代码没有WordPress PHP代码

get_image();
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 代码,它给 Ajax 调用上面的 PHP 片段

<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() {
            //console.log(this.response);
            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>

让我们进入 WordPress... HTML 部分

<div><input type="button" name="ajax_get_image_button" id="ajax_get_image_button" value="Ajax call to get image"  class="button-primary"/>

JS 部分

    $("#ajax_get_image_button").click(function() {        
        var data = {                    
                _ajax_nonce: my_js_obj.nonce, 
                action: "func_get_image",
            };
        url=my_js_obj.ajax_url;
        //xhr
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.responseType = "blob";
        xhr.onload = function() {
            var img = document.createElement('img');
            img.src=URL.createObjectURL(this.response); 
            document.body.append(img);
        };

        xhr.send($.param( data ));
    });

PHP部分

add_action('admin_enqueue_scripts','my_javascript');
function my_javascript($hook) {
    global $current_screen;
    if( $hook !== $current_screen->base ) return; //my page see this script only
        wp_enqueue_script( 
        'my_js', 
        plugins_url( 'js/get_image.js', __FILE__ ),
        array('jquery'), 
        '1.0.0',
    );
    wp_localize_script(
        'my_js',
        'my_js_obj',
        array(
            'ajax_url' => admin_url('admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'my_nonce' ),
        )
    );

}
...
add_action( 'wp_ajax_my_get_image', 'func_get_image' );
function func_get_image(){
    check_ajax_referer( 'my_nonce');
    get_image();
    wp_die();
}

我以为 WordPress 只是挂钩并执行我的脚本,所以它应该像我在常规 PHP 代码中预期的那样返回结果。但事实并非如此。尝试查找此问题的任何解决方案,但几天后似乎没有找到匹配项。

溶液

readfile(plugin_dir_path( __FILE__ ) . '/'. $file);
Ajax wordpress 下载 二进制文件

评论

0赞 O. Jones 11/6/2023
下一个调试步骤:将 error_log() 调用放入各种 php 函数中,以便您知道何时调用了哪些代码。更好的是,使用 php 调试器。
0赞 pang-db-builder 11/8/2023
我现在想通了。我有 2 个问题。第一个<?php是因为我得到了一个以<?php<?php开头的PHP文件。一旦我解决了这个问题,我很快就意识到 WordPress 需要 PHP 以完整路径读取文件。它的意思是 readfile(plugin_dir_path( FILE ) 。'/'.$file);是关键。

答: 暂无答案