如何使用 PHP 或 HTML 在浏览器中查看/打开 Word 文档

发布于 2024-10-06 01:08:11 字数 56 浏览 0 评论 0原文

如何在浏览器中打开并查看 .doc 文件扩展名?该文件位于我的服务器上。

How can I open and view a .doc file extension in my browser? The file is located on my server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

有两个选项:第一个是仅链接到它,例如 My Word Document,第二个是使用 iframe 并将其指向该文件。然而,要实现此功能,大多数浏览器要求服务器随文档一起发送 Content-disposition: inline 标头。如果您无法配置您的 Web 服务器来执行此操作,您可以将文档包装在一些 php: 中

<?php
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;

,然后链接到该脚本而不是您的 Word 文档。

但这并不能保证一定有效; content-disposition 标头只是一个提示,任何浏览器都可以选择将其视为附件。

另外,请注意 .doc 并不完全可移植;基本上,您需要 Word 才能正确显示它(Open Office 和其他一些开源应用程序做得不错,但还不够),并且浏览器必须支持将 Word 作为插件打开。

如果 .doc 文件格式要求不是一成不变的,PDF 将是更好的选择(转换通常就像在 PDF 打印机上打印一样简单,例如,CutePDF,从 Word 内部),或者您甚至可以转换将文档转换为 HTML(尽管情况可能会有所不同)。

Two options: First is to just link to it, e.g. <a href="MyWordDocument.doc">My Word Document</a>, the second is to use an iframe and point it to the document. For this to work, however, most browsers require that the server sends a Content-disposition: inline header with the document. If you cannot configure your web server to do this, you can wrap the document in a bit of php:

<?php
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;

And then link to that script instead of your word document.

This isn't guaranteed to work though; the content-disposition header is just a hint, and any browser may choose to treat it as an attachment anyway.

Also, note that .doc isn't exactly portable; basically, you need Word to display it properly (Open Office and a few other Open Source applications do kind of a decent job, but they're not quite there yet), and the browser must support opening Word as a plugin.

If the .doc file format requirement isn't set in stone, PDF would be a better choice (the conversion is usually as simple as printing it on a PDF printer, say, CutePDF, from inside Word), or maybe you can even convert the document to HTML (mileage may vary though).

执手闯天涯 2024-10-13 01:08:12
<a href="foo.doc">…</a>

您将需要一个安装了 Office 文档插件的浏览器。我相信 Microsoft Office 默认情况下至少会为 Internet Explorer 安装一个。

如果您想在没有插件的情况下工作,那么您需要将文档转换为另一种格式 - HTML 以获得最大兼容性。这不是一个简单的操作,特别是对于复杂的文档(甚至那些只包含图像的文档)。

<a href="foo.doc">…</a>

You will need a browser with a plugin for Office documents installed. I believe Microsoft Office will install one for at least Internet Explorer by default.

If you want to work without a plugin, then you will need to convert the document to another format — HTML for maximum compatibility. This isn't a trivial operation, especially for complex documents (or even those which just contain images).

2024-10-13 01:08:12
$file = "$file_name.doc";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type:application/zip"); // Send type of file
$header="Content-Disposition: attachment; filename=$patient_name.zip;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
$file = "$file_name.doc";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type:application/zip"); // Send type of file
$header="Content-Disposition: attachment; filename=$patient_name.zip;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
淡看悲欢离合 2024-10-13 01:08:12

您可以使用谷歌文档,因为它是免费且可靠的
您可以将文件路径分配给 iframe。

例如 iframe1.Attributes.Add("Src", "http://docs.google. com/gview?url=http://YOUR_FILE_PATH&embedded=true");

You can use google docs instead as it is free and reliable
You can assign your file path to iframe.

e.g. iframe1.Attributes.Add("Src", "http://docs.google.com/gview?url=http://YOUR_FILE_PATH&embedded=true");

辞慾 2024-10-13 01:08:12

如果您的 .doc 文件可以在线访问,您可以尝试 Office Web 查看器 服务。

如果您的文档存储在 Intranet 中,您可以使用 Microsoft Office Web Apps Server。它允许用户通过浏览器查看Word、PowerPoint、Excel 文档。

If your .doc file is accessable online, you can try Office Web Viewer service.

If your documents stored in Intranet, you can use Microsoft Office Web Apps Server. It allows users to view Word, PowerPoint, Excel documents via browser.

今天小雨转甜 2024-10-13 01:08:12
//Edit
$header="Content-Disposition: attachment; filename=$file_name.doc;"; // Send File Name
//Edit
$header="Content-Disposition: attachment; filename=$file_name.doc;"; // Send File Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文