【问题标题】:Automatically open PDF after force download in PHP在 PHP 中强制下载后自动打开 PDF
【发布时间】:2015-04-20 16:54:33
【问题描述】:

我有以下来自 HTML 应用程序的 PHP 代码(用户在 Android 上使用 Chrome 浏览器)

$path_full = '../files/this_is_my_dir/test.pdf';
$path_exploded = explode('/', $path_full);
$filename = end($path_exploded);

if (file_exists($path_full)) {
    $finfo  = new finfo(FILEINFO_MIME);

    header('Content-Description: File Transfer');
    header('Content-Type: ' . $finfo->file($path_full));
    header("Content-Disposition:attachment;filename='" . $filename . "'");
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path_full));
    readfile($path_full);
}

当用户点击一个图标时,它会强制下载(使用上面的代码)文件(位于公共根目录之外)到 android 下载文件夹。但是我们希望 PDF 在下载后自动打开(就像单击一个 href 链接一样)。这可能吗?

【问题讨论】:

    标签: php android pdf download


    【解决方案1】:

    打开一个意图:

    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
    

    This是来源

    【讨论】:

    • 感谢您的评论。但我不是在开发原生应用程序,而是在开发 HTML 应用程序。
    猜你喜欢
    • 2021-10-30
    • 2013-12-07
    • 2019-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2012-05-21
    相关资源
    最近更新 更多