【问题标题】:how to display word document in browser in localhost?如何在本地主机的浏览器中显示word文档?
【发布时间】:2016-06-17 18:10:12
【问题描述】:

我从 stackoverflow 获得了这段代码,它可以在浏览器中显示 word 文档.. 是否可以仅在 localhost 中显示 word 文档.. 尝试此代码后 bcz .​​. 它只显示“没有可用的预览”。 .

$(document).ready(function() {
$(".word").fancybox({
'width': 600, // or whatever
'height': 320,
'type': 'iframe'
});
}); 

<a class="word" href="http://docs.google.com/gview?url=http://domain.com/path/docFile.doc&embedded=true">open a word document in fancybox</a>

我的作品

$path="resume/".$row['resume']; 
echo "<br /><a class='word' href='http://docs.google.com/gview?url=".$path."&embedded=true'>View Resume</a>";

我给的路径不正确?

【问题讨论】:

  • $path = http://domain.com/path/docFile.doc ?? echo $path查看结果
  • 工作正常吗? http://docs.google.com/gview?url=http://domain.com/path/docFile.doc&amp;embedded=true如果你点击浏览器
  • 没有 .. 它只显示没有可用的预览
  • 在浏览器中点击这个网址:domain.com/path/docFile.doc,工作吗?

标签: javascript php mysql


【解决方案1】:

你不能。浏览器没有任何内置方式来查看 Word 文档,因此除非用户已将浏览器配置为使用某些插件打开它(世界上 99% 的人还没有这样做),否则浏览器会提示他们下载文件.

因此,目前没有浏览器具有呈现 Word 文档所需的代码,而且据我所知,目前也没有用于呈现它们的客户端库。

您可以通过

使用 Google 文档查看器
<iframe src="http://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

您可以在SO link查看解决方案

如果你想使用下载脚本打开文件,那么你可以使用

内容类型:application/vnd.ms-word

<?php
header('Content-type: application/vnd.ms-word');
header('Content-Disposition: attachment; filename="document.doc"');
readfile('path-to-file.docx');
?>

您可以使用office live apps viewer ,需要互联网连接://view.officeapps.live.com/op/embed.aspx?src=your_url_here

将此网址放在 iframe 中

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http://remote.url.tld/path/to/document.doc' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

如果您只想显示内容,则可以将 word 文件转换为 pdf 文件类型。您可以使用cloud convert 将文件从一种格式转换为另一种格式。目前云转换支持多达 128 种不同的文件格式。

还有一个SO link for file format conversion

【讨论】:

  • 您的具体要求是什么。您是否也只想查看或编辑该文档?
  • 我只想在浏览器中查看 word 文档。我想先在 localhost 上进行
  • 刚刚编辑了我的答案。请检查一次 view.officeapps.live.com
  • 但是这段代码正在下载我的word文件..我们可以将ms word转换为文本文档并在浏览器上显示..我的意思是它不需要只在ms word中显示..跨度>
  • 如果您只想显示内容,那么您可以将 word 文件转换为 pdf 或任何其他文件格式。刚刚编辑了我的答案
【解决方案2】:

$path 必须是完整的外部路径,以便 google 文档可以访问它。您不能为此使用相对路径。该文档必须面向 Internet 才能使用此代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多