编程语言
首页 > 编程语言> > php-无法在Google Doc Viewer中嵌入.doc,docx文件

php-无法在Google Doc Viewer中嵌入.doc,docx文件

作者:互联网

我有将代码嵌入页面中并在Google Docs Viewer中打开的代码.但是到目前为止,当我想打开.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 );

解决方法:

NOTE: please read full answer including the edits down below

Google文档查看器似乎可以正常运行.出于测试目的,我使用了该文档: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 Docs Viewer,不会出现问题.

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

编辑2(2019)

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

<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"></iframe>

标签:google-docs-api,php,jquery
来源: https://codeday.me/bug/20191118/2030983.html