如何使用 JavaScript 在浏览器中呈现 Word 文档(.doc、.docx)? [英] How do I render a Word document (.doc, .docx) in the browser using JavaScript?

查看:71
本文介绍了如何使用 JavaScript 在浏览器中呈现 Word 文档(.doc、.docx)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功完成了在浏览器中显示 PDF 文件而不是打开/保存"对话框的代码.现在,我一直在尝试在浏览器中显示 Word 文档.我想在 Firefox、IE7+、Chrome 等中显示 Word 文档.

I have successfully done code to display a PDF file in the browser instead of the "Open/Save" dialog. Now, I'm stuck trying to display a Word document in the browser. I want to display a Word document in Firefox, IE7+, Chrome etc.

有人可以帮忙吗?在浏览器中显示 Word 文档时,我总是收到打开/保存"对话框.我想使用 JavaScript 实现此功能.

Can any one help? I am always getting the "Open/Save" dialog while displaying the Word doc in browser. I want to implement this functionality using JavaScript.

推荐答案

目前没有浏览器具有渲染 Word 文档所需的代码,据我所知,目前也没有用于渲染它们的客户端库.

No browsers currently have the code necessary to render Word Documents, and as far as I know, there are no client-side libraries that currently exist for rendering them either.

但是,如果您只需要显示 Word 文档,而不需要对其进行编辑,则可以通过 <iframe> 使用 Google 文档的查看器来显示远程托管的 <代码>.doc/.docx.

However, if you only need to display the Word Document, but don't need to edit it, you can use Google Documents' Viewer via an <iframe> to display a remotely hosted .doc/.docx.

<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

解决方案改编自"如何使用fancybox显示word文档".

Solution adapted from "How to display a word document using fancybox".

示例:

JSFiddle

但是,如果您希望在大多数(如果不是所有)浏览器中获得本机支持,我建议将 .doc/.docx 重新保存为 PDF文件也可以使用 Mozilla 的 PDF.js 独立呈现.

However, if you'd rather have native support, in most, if not all browsers, I'd recommend resaving the .doc/.docx as a PDF file Those can also be independently rendered using PDF.js by Mozilla.

非常感谢 cubeguerrero 在评论中发布 Microsoft Office 365 查看器.

Huge thanks to cubeguerrero for posting the Microsoft Office 365 viewer in the comments.

<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>

正如 lightswitch05 所指出的,要记住的另一个重要警告是,这会将您的文档上传到第三方服务器.如果这是不可接受的,那么这种展示方式就不是正确的做法.

One more important caveat to keep in mind, as pointed out by lightswitch05, is that this will upload your document to a third-party server. If this is unacceptable, then this method of display isn't the proper course of action.

现场示例:

Google 文档查看器

Microsoft Office 查看器

这篇关于如何使用 JavaScript 在浏览器中呈现 Word 文档(.doc、.docx)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆