在PHP中强制下载后自动打开PDF

时间:2015-02-19 11:09:04

标签: php android pdf download

我有来自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链接一样)。这可能吗?

1 个答案:

答案 0 :(得分:0)

打开一个意图:

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是来源

相关问题