文件下载不是从JQuery开始的

时间:2016-10-09 09:15:15

标签: php jquery

我正在尝试使用JQuery开始下载文件。该文件由.php函数创建。

下载无法启动。它只在我手动打开.php文件时启动(注释isset并手动将数据写入文件)。

这是JQuery代码:

$("#experimenter_dataset").click(function() {
                var imei = $("#select_download_experimenters option:selected").text();
                $.post("download_imei_activities.php", { imei:imei }).done(function( data ) {

                });
            });

这是php代码

if(isset($_POST["imei"])) {

    $imei = $_POST["imei"];

    $stmt = $pdo->prepare("SELECT * FROM performedactivity WHERE experimenter=:imei");
    $stmt->bindParam(":imei", $imei, PDO::PARAM_STR);

    //$output = $imei . ".txt";
    $output = "";

    if ($stmt->execute()) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $activity = $row["activity"];
            $timestamp_start = $row["timestampstart"];
            $timestamp_stop = $row["timestampstop"];

            $date_hour_start = explode(" ", $timestamp_start);
            $date_hour_stop = explode(" ", $timestamp_stop);

            //$current = "";

            $output .= $date_hour_start[0] . "\t" . $date_hour_start[1] . "\t" . $activity . "\t" . "START" . "\r\n";
            $output .= $date_hour_stop[0] . "\t" . $date_hour_stop[1] . "\t" . $activity . "\t" . "END" . "\r\n";

            //file_put_contents($output, $current, FILE_APPEND | LOCK_EX);
        }
    } else {
        //echo json_encode($pdo->errorInfo());
    }

    // We'll be outputting a text file
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename='.$imei . ".txt");
    header("Content-Transfer-Encoding: binary");
    print($output);
    //echo $output;
    /*header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename= " . $output);
    header("Content-Transfer-Encoding: binary");
    readfile($output);*/
} else {
    //echo "imei parameter is missing";
}

0 个答案:

没有答案