使用JavaScript和PHP生成文件

使用JavaScript和PHP生成文件,javascript,php,jquery,Javascript,Php,Jquery,我正在尝试生成一个要下载的文件。使用JavaScript,我调用一个PHP文件来处理我的请求,并以一种应该可以下载的方式返回结果。但是它不提供下载,只显示代码 PHP function export() { // Get a database object. $db = JFactory::getDbo(); // Create a new query object. $query = $db->getQuery(true); // Sel

我正在尝试生成一个要下载的文件。使用JavaScript,我调用一个PHP文件来处理我的请求,并以一种应该可以下载的方式返回结果。但是它不提供下载,只显示代码

PHP

function export()
{
    // Get a database object.   
    $db = JFactory::getDbo();

    // Create a new query object.
    $query = $db->getQuery(true);

    // Select fields to get.
    $fields = array(
        $db->quoteName('params')
    );

    // Conditions for which records should be get.
    $conditions = array(
        $db->quoteName('element') . ' = ' . $db->quote('plugin_name'), 
        $db->quoteName('folder') . ' = ' . $db->quote('system')
    );

    // Set the query and load the result.
    $query->select($fields)->from($db->quoteName('#__extensions'))->where($conditions);
    $db->setQuery($query);   
    $results = $db->loadResult(); 

    // Namming the filename that will be generated.
    $name      = 'file_name';
    $date      = date("Ymd");
    $json_name = $name."-".$date;

    // Clean the output buffer.
    ob_clean();

    echo $results;
    header('Content-disposition: attachment; filename='.$json_name.'.json');
    header('Content-type: application/json');
}
JavaScript

function downloadFile() {
    var fd = new FormData();
    fd.append('task', 'export');
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", uploadComplete, false);
    xhr.open("POST", "my_php_file");
    xhr.send(fd);
}
HTML文件

<button class="btn btn-primary btn-success" type="button" onclick="downloadFile()"></button>


更新我的代码

在输出数据之前,您需要调用任何标题函数调用。否则,您将收到标题“headers ready sent”(标题已发送)警告,并且不会设置标题

示例:

...
// Namming the filename that will be generated.
$name      = 'file_name';
$date      = date("Ymd");
$json_name = $name."-".$date;

header('Content-disposition: attachment; filename='.$json_name.'.json');
header('Content-type: application/json');

// Clean the output buffer.
ob_clean();

echo $results;
一个例子

<?php
ob_start();

echo "some content to go in a file";

$contentToGoInFile = ob_get_contents(); //this gets the outputted content above and puts it into a varible/buffer
ob_end_clean();
header('Content-disposition: attachment; filename='.$json_name.'.json');
header('Content-type: application/json');
echo $contentToGoInFile;
exit; //stops execution of code below

此外,我还看到了ob_clean(),我假设在缺少的代码中,您在某个地方有ob_start(),“它只是显示代码”-是您的文件
.php
扩展名吗?是的,您缺少ob_start()”输出缓冲区必须由带有php_output_HANDLER_CLEANABLE标志的ob_start()启动。否则ob_clean()将不起作用。请参见下面的“代码”示例正在展示什么?你能用静态内容而不是DB查询来做一个例子吗?是的,我的猜测是,它没有将文件解释为PHP,正如@Fred ii所问的,文件是否有PHP扩展名,PHP是否确实安装在服务器上?如果使用phpinfo()创建新的php文件phpinfo.php;你得到什么结果了吗?@user4076077那么问题一定在你代码中省略的部分。你能增加一点娱乐活动吗?
$results = $db->loadResult(); 

// Namming the filename that will be generated.
$name      = 'file_name';
$date      = date("Ymd");
$json_name = $name."-".$date;

ob_start();
echo $results;
$contentToGoInFile = ob_get_contents(); //this gets the outputted content above and puts it into a varible/buffer
ob_end_clean();
header('Content-disposition: attachment; filename='.$json_name.'.json');
header('Content-type: application/json');
echo $contentToGoInFile;
exit