无法在google doc viewer中嵌入.doc,docx文件

时间:2016-04-13 08:19:04

标签: php jquery google-docs-api

我的代码可以在页面中嵌入文件并在Google文档查看器中打开。但到目前为止,我得到了#34;无法预览"当我想打开.doc或docx文件,但打开PDF文件时打开正确。 而且,我不再在Google文档中打开文件,而是提示您下载文件而不是在浏览器上查看文件。

这是我的代码:

<a href="sample.doc"  class="embed"><h2>sample.doc</h2><button >view document</button></a>
<script>
    $(document).ready(function() {
      $('a.embed').gdocsViewer({width:740,height:742});
      $('#embedURL').gdocsViewer();
    });
</script>

这是我的jQuery插件:

(function($){
    $.fn.gdocsViewer = function(options) {

        var settings = {
            width  : '600',
            height : '700'
        };

        if (options) { 
            $.extend(settings, options);
        }

        return this.each(function() {
            var file = $(this).attr('href');
            var ext = file.substring(file.lastIndexOf('.') + 1);

            if (/^(tiff|doc|ppt|pps|pdf|docx)$/.test(ext)) {
                $(this).after(function () {
                    var id = $(this).attr('id');
                    var gdvId = (typeof id !== 'undefined' && id !== false) ? id + '-gdocsviewer' : '';
                    return '<div id="' + gdvId + '" class="gdocsviewer"><iframe src="http://docs.google.com/viewer?embedded=true&url=' + encodeURIComponent(file) + '" width="' + settings.width + '" height="' + settings.height + '" style="border: none;margin : 0 auto; display : block;"></iframe></div>';
                })
            }
        });
    };})( jQuery );

2 个答案:

答案 0 :(得分:2)

Google Docs Viewer似乎运行得很好。出于测试目的,我使用了此文档:http://homepages.inf.ed.ac.uk/neilb/TestWordDoc.doc

直接从新标签页访问https://docs.google.com/viewer?embedded=true&url=http%3A%2F%2Fhomepages.inf.ed.ac.uk%2Fneilb%2FTestWordDoc.doc会导致标准Google文档查看器出现问题。

从iframe内部进行测试的方式与此相同:

<iframe src="https://docs.google.com/viewer?embedded=true&url=http%3A%2F%2Fhomepages.inf.ed.ac.uk%2Fneilb%2FTestWordDoc.doc" frameborder="no" style="width:100%;height:160px"></iframe>

总之,

Google在文档查看器中没有更改任何内容。我的猜测是你没有在你的href标签中使用绝对URL 。因此,在您的代码中,将第一行更改为以下内容:

<a href="http://www.yourwebsite.com/directory/sample.doc" class="embed"><h2>sample.doc</h2><button >view document</button></a>ç

修改

如果之前的代码不适合您,请使用以下代码替换iframe代码,尝试较新的Google云端硬盘查看器:

<iframe src="https://docs.google.com/viewerng/viewer?url=YOUR_DOCUMENT_URL"></iframe>

答案 1 :(得分:1)

Google Docs viewer似乎不再起作用了。 Google现在使用Drive Rest API,并且需要将文档转换为Google格式。

如果您想嵌入Google doc文件。转到文件 - &gt;发布到Web并选择“嵌入”选项。它将生成一个嵌入代码段。