1 设置kkFileView

kkFileView开源的office在线预览项目,真的很不错!官网地址如下:

https://kkfileview.keking.cn/zh-cn/index.html

# 下载镜像
sudo docker pull keking/kkfileview:v4.0.0

# 启动kkFileView
sudo docker run -itd --name kkfileview -p 8012:8012 keking/kkfileview:v4.0.0

2 设置Nginx文件代理

设置容器

# 创建nginx容器
sudo docker run -itd --name kkfileview_nginx -v /home/yyp/file:/home/file -p 80:80 nginx:1.20.0

# 将容器中default.conf复制到本地
sudo docker cp kkfileview_nginx:/etc/nginx/conf.d/default.conf .

# 将default.conf复制到容器中
sudo docker cp default.conf kkfileview_nginx:/etc/nginx/conf.d

nginx配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /file/ {
        alias   /home/file/;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

3 设置HTML文件

!!!注意!!!

如果使用容器文件中的url = "http://172.17.0.3/file/my_data.docx";的ip地址不能是127.0.0.1。ip地址必须是一个与kkFileView容器在同一网段,并且能够下载文件的ip地址。

我的kkfileview容器的ip地址是:172.17.0.2;nginx容器的ip地址是:172.17.0.3。

<!DOCTYPE html>
<html>
        <head>
                <title>测试</title>
        </head>
        <script src="https://cdn.jsdelivr.net/npm/js-base64@3.6.0/base64.min.js"></script>
        <script type="text/javascript">
                // npm在线安装
                // npm install --save js-base64@3.6.0
                function jump(){
                        //要预览文件的访问地址
                        var url = "http://172.17.0.3/file/my_data.docx";
                        window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
                }
        </script>

        <body>
                <input type="button" value="查看文件" onclick="jump()" />
        </body>
</html>

4 截图

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐