如何使用 PHP 或 HTML 在浏览器中查看/打开 Word 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4346117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I view/open a word document in my browser using with PHP or HTML
提问by Ruben
How can I open and view a .doc
file extension in my browser? The file is located on my server.
如何.doc
在浏览器中打开和查看文件扩展名?该文件位于我的服务器上。
回答by tdammers
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:
两个选项:第一个是只链接到它,例如<a href="MyWordDocument.doc">My Word Document</a>
,第二个是使用 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;
And then link to that script instead of your word document.
然后链接到该脚本而不是您的 Word 文档。
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.
但这并不能保证工作;content-disposition 标头只是一个提示,任何浏览器都可以选择将其视为附件。
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.
另外,请注意 .doc 并不是完全可移植的;基本上,您需要 Word 才能正确显示它(Open Office 和其他一些开源应用程序做得不错,但还没有完全实现),并且浏览器必须支持将 Word 作为插件打开。
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).
如果 .doc 文件格式要求不是一成不变的,PDF 将是更好的选择(转换通常就像在 PDF 打印机上打印一样简单,例如,从 Word 内部的CutePDF),或者您甚至可以转换将文档转换为 HTML(尽管里程可能会有所不同)。
回答by Quentin
<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.
您将需要一个带有 Office 文档插件的浏览器。我相信默认情况下,Microsoft Office 至少会为 Internet Explorer 安装一个。
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).
如果您想在没有插件的情况下工作,那么您需要将文档转换为另一种格式——HTML 以获得最大的兼容性。这不是一个微不足道的操作,尤其是对于复杂的文档(甚至那些只包含图像的文档)。
回答by yesnik
If your .doc file is accessable online, you can try Office Web Viewerservice.
如果您的 .doc 文件可在线访问,您可以尝试Office Web Viewer服务。
If your documents stored in Intranet, you can use Microsoft Office Web AppsServer. It allows users to view Word, PowerPoint, Excel documents via browser.
如果您的文档存储在 Intranet 中,您可以使用Microsoft Office Web AppsServer。它允许用户通过浏览器查看 Word、PowerPoint、Excel 文档。
回答by Hardik Mali
You can use google docs instead as it is free and reliable You can assign your file path to iframe.
您可以改用 google docs,因为它免费且可靠您可以将文件路径分配给 iframe。
e.g. iframe1.Attributes.Add("Src", "http://docs.google.com/gview?url=http://YOUR_FILE_PATH&embedded=true");
例如 iframe1.Attributes.Add("Src", " http://docs.google.com/gview?url=http:// YOUR_FILE_PATH&embedded=true");
回答by emeka
$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);
回答by emeka
//Edit
$header="Content-Disposition: attachment; filename=$file_name.doc;"; // Send File Name