【问题标题】:Cannot embed .doc,docx files in google doc viewer无法在谷歌文档查看器中嵌入 .doc、docx 文件
【发布时间】:2019-06-11 16:01:44
【问题描述】:

我有将文件嵌入页面并在 Google Docs Viewer 中打开的代码。但到目前为止,当我想打开 .doc 或 docx 文件但打开 PDF 文件时正确打开时,我得到“没有可用的预览”。 再次提示我下载文件而不是在浏览器上查看文件,而不是在 Google Docs 中打开文件。

这是我的代码:

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

【问题讨论】:

    标签: php jquery google-docs-api


    【解决方案1】:

    注意:请阅读完整答案,包括下面的编辑

    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 内部测试它的工作方式相同:

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

    总之,

    Google 在 Docs Viewer 中没有任何改变。我的猜测是您没有在 href 标记中使用 absolute URL。因此,在您的代码中,将第一行更改为如下内容:

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

    编辑

    如果之前的代码对您不起作用,请尝试使用更新的 Google Drive 查看器,将 iframe 代码替换为:

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

    编辑 2 (2019)

    Google Docs Viewer 似乎有一半时间加载文档,在这些情况下必须重新加载 iframe,直到它正确显示。因此,最好使用 Office Web Apps Viewer 作为替代方案:

    &lt;iframe src="https://view.officeapps.live.com/op/embed.aspx?src=http%3A%2F%2Fhomepages.inf.ed.ac.uk%2Fneilb%2FTestWordDoc.doc" frameborder="no" style="width:100%;height:500px"&gt;&lt;/iframe&gt;

    【讨论】:

    • 我不知道怎么了@josemmo,但是当我运行你发送的链接时,我会收到提示下载文件,当我使用你所说的有效网址时“yourwebsite.com/directory/sample.doc”我得到一个“框架中没有可用的预览”错误。
    • 嗨@user2770155!请尝试访问以下 URL:docs.google.com/viewerng/…。如果这个可行,我将编辑我的答案。
    • 好的,让我检查一下。现在!!
    • 嘿,我检查了一下,它打开得很好。
    • @josemmo,有一个场景,当点击该文件时列出了许多文件,它会正确打开,但在极少数情况下,它不会更新最近使用谷歌驱动器预览器点击的文件,面临这样的情况设想?谢谢
    【解决方案2】:

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

    如果你想嵌入一个谷歌文档文件。转到文件-> 发布到 Web 并选择嵌入选项。它会生成一个嵌入的sn-p代码。

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 2017-11-06
      相关资源
      最近更新 更多