【问题标题】:ajax post then get file to useajax post 然后获取要使用的文件
【发布时间】:2017-01-15 17:09:16
【问题描述】:

我是 php 新手,我正在编写一个代码,它允许我向 php 发送一些数据,然后执行一些exec(),然后让进程输出返回使用(例如发布一个网页)。

$.ajax({
        url: 'some.php',
        type: 'POST',
        data: someData,
        async: false,
        success: function (html) {
            $('#showonscreen').html(html);
        },
        cache: false,
        contentType: false,
        processData: false
    });

在 php 部分就像

<?php
/*something like $cmd here*/
    exec("$cmd $target_file $Output_fileName $upper $lower");
    echo "<br/>done";
?>

之后 exec() 会在根文件夹下生成一个带有该 Output_fileName 的输出文件(例如图像)。 我的问题是我可以获取(或加载?)此文件直接返回客户端使用吗?(成功部分?)或者我需要 $.get 再次从服务器请求以获取该文件?

所以,在客户端,它的工作原理是,他们上传一张图片,然后单击按钮对其进行处理,然后将结果图片存储在服务器根目录下,然后从中抓取以显示在网页上。

任何帮助

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

    在服务器端

    首先,如果您像下面这样捕获 o/p,那就太好了

    exec('your_command 2>&1', $outputAndErrors, $return_value);
    

    然后

    if (!$return_value) {
    
        // Ok lets decide what next
    
    } else {
    
        // Ooops some problem error handling
    }
    

    正如你提到的,如果你正在处理图像,那么

    echo base64_encode(file_get_contents($your_file_generated));
    

    对于纯文本,您可以

    header('Content-type: text/plain; charset=utf-8');
    
    echo file_get_contents($your_file_generated);
    
     // OR
    
    echo readfile($your_file_generated);
    

    在客户端

    $.ajax({
        ..
        ..
        success: function (server_response) {
            $('#showonscreen').html('<img src="data:image/png;base64,'+ server_response +'"/>');
        },
        ..
        ..
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多