未指定下载大小

时间:2014-02-17 12:06:51

标签: php http-headers download

我想下载PDF文件:

$output = RP_MAIN . 'docbook/data/myfile' . $_SESSION["sess_code_user"] . '.pdf'; // this is the pdf file I want to download
$file_size = filesize($output);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-type: text/enriched");
header("Content-length: $file_size");
header('Content-Disposition: attachment; filename="'.'manuel.'. $extension .''.'"');
ob_clean();
flush();
print($output);
exit();

PDF文件存在于系统中,大小为25 Kb。但问题是下载弹出窗口(Internet Download Manager)在大小字段中有一个空白字段。

我的代码出了什么问题?我该如何解决这个问题?


编辑: pdf文件是从ajax调用创建的,然后在ajax成功的时候开始下载:

var request = $.ajax({
        data: donne,
        type: "POST",
        url:  "<?php echo HTTP_AJAX_TACHE ?>TacheCreerPdfAjax.php" , // this creates the pdf file
        async: false
    });

    request.done(function() { 
        $('#corps').html(request.responseText);

        if(fichier == "pdf" || fichier == "rtf")
            window.location.href = '<?php echo PAGE_TACHE ?>?action=TacheGenererManuelExec&fichier=' + fichier ; // this starts the download
    ...
    }

因此,在启动下载时会出现错误,指出源的格式不是pdf!

1 个答案:

答案 0 :(得分:0)

检查一下:

$output = RP_MAIN . 'docbook/data/myfile' . $_SESSION["sess_code_user"] . '.pdf';    
if (is_file($output)) {
    $size = filesize($output);
    if (function_exists('mime_content_type')) {
        $type = mime_content_type($output);
    } elseif (function_exists('finfo_file')) {
        $info = finfo_open(FILEINFO_MIME);
        $type = finfo_file($info, $output);
        finfo_close($info);
    }
    if ($type == '') {
        $type = "application/force-download";
    }
    header("Content-Type: $type");
    header("Content-Disposition: attachment; filename=$name");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . $size);
    readfile($output);
    exit;
} else {
    return false;
}