【问题标题】:Download size is not specified未指定下载大小
【发布时间】:2014-03-16 17:24:51
【问题描述】:

我想下载一个 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!

【问题讨论】:

    标签: php http-headers download


    【解决方案1】:

    检查一下:

    $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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      相关资源
      最近更新 更多