直链打开word系列的文件会触发下载链接:
<a href="example.docx">点击这里打开Word文档</a>
故而我采用了三种方法:
注意:支持的 MIME 类型: 要在网页中打开 Word 文档,需要指定正确的 MIME 类型。下面是一些常见的 Word 文档的 MIME 类型: .doc 扩展名的 Word 文档:application/msword .docx 扩展名的 Word 文档:application/vnd.openxmlformats-officedocument.wordprocessingml.document 如果不确定 Word 文档的 MIME 类型,可以通过以下方式获取: 打开 Windows 的“文件资源管理器”,找到 Word 文档。 右键点击 Word 文档,选择“属性”。 在“属性”窗口中的“类型”字段中找到 MIME 类型。
HTML5 提供了 <object> 标签,通过它我们可以在网页中嵌入多媒体内容,包括 Word 文档。下面是一个示例代码,展示如何使用 <object> 标签嵌入 Word 文档:
<object data="path/to/word.docx" type="application/vnd.openxmlformats-officedocument.wordprocessingml.document">
<p>抱歉,您的浏览器不支持打开 Word 文档。</p>
</object>
这种方法虽然大众并且网上资料大部分都是该做法,但是我使用下来发现好像在大部分的浏览器或者设备中可能不受到支持,我一开始使用的时候在支持html5协议的谷歌浏览器/edge浏览器都显示不支持插件,故而采用下面2种方法。
这是我第一次改进后方法的代码,采用头文件的方式为浏览器设置响应头,根据响应头来决定是否打开文件,Content-Disposition: inline;属性来决定文件不执行下载而是打开,但是有些浏览器比如edge支持并且打开offer的网页方式,但是国内的谷歌浏览器可能没有配置合适的打开手段插件,所以这个可能不执行或者执行下载,这种情况可以用也可以采用方法3(全部适配浏览器)。
<?php
$file = $_GET['filename'];
$file_path = __DIR__ . '/拼接字串/' . $file;
$file_ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
// 尝试根据文件扩展名确定 MIME 类型
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'pdf' => 'application/pdf',
'doc' => 'application/msword', // 对于 .doc 文件
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' // 对于 .docx 文件
// ... 添加其他 MIME 类型
);
$mime_type = isset($mime_types[$file_ext]) ? $mime_types[$file_ext] : 'application/octet-stream';
// 检查是否设置了下载参数
$download = isset($_GET['download']) && $_GET['download'] == 'true';
if (file_exists($file_path)) {
if ($download) {
// 设置头信息以触发文件下载
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: inline; filename="' . basename($file_path) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
} else {
// 获取文件的MIME类型
// $finfo = new finfo(FILEINFO_MIME);
// $mime_type = $finfo->file($file_path);
// 设置头信息以直接打开文件
header('Content-Type: ' . $mime_type);
header('Content-Disposition: inline; filename="' . basename($file_path) . '"');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
}
exit;
} else {
// 没有权限,显示错误信息
echo "You do not have permission to access this file."; //$file_path
exit;
}
?>
这里是最后改进后的代码,这里采用识别到是doc、docx(ppt/pdf也可以使用这个方法)的情况下采用office的支持网址链接形式打开,这种方法来自office的网站支持使用适配性最好,基本都可以使用。
if ($file_ext === ['doc', 'docx']) {
if ($_GET['download'] === 'true') {
// 下载文件
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
} else {
// 构建Office在线预览链接
$url = "https://view.officeapps.live.com/op/view.aspx?src=http://" . $_SERVER['HTTP_HOST'] . "/wenjianjia/{$file}&download=false&wdOrigin=BROWSELINK";
header("Location: $url");
exit;
}
}
本文介绍了三种在网页中打开 Word 文档的方法,包括使用 <object> 标签、设置响应头和构建 Office 在线预览链接。这些方法旨在适应不同浏览器和设备的支持情况,确保用户能够顺利查看 Word 文档。