【uniapp】微信小程序-实现文件查看和分享功能

代码

<button @click="viewFile">查看文件</button>
<button @click="shareFile">分享给好友</button>
//查看文件
viewFile(){
    const _this = this
    //如果该文件已经下载可以直接打开
    if(this.filePath){
        uni.openDocument({
            filePath: _this.filePath,
            showMenu: true,
            success: function (res) {
                console.log('打开文档成功');
            }
        });
    }else{
        //否则首先调用下载方法
        uni.downloadFile({
            //此处url为需要分享的文件的url
            url: 文件url,
            success: function (res) {
                _this.filePath = res.tempFilePath;
                uni.openDocument({
                    filePath: _this.filePath,
                    showMenu: true,
                    success: function (res) {
						    console.log('打开文档成功');
				    }
                });
            }
        });
    }
},
shareFile(){
    const _this = this
    //如果该文件已经下载可以直接分享
    if(this.filePath){
        wx.shareFileMessage({
            filePath: _this.filePath,
            success() {},
            fail: console.error,
        })
    }else{
        //否则首先调用下载方法
        uni.downloadFile({
            //此处url为需要分享的文件的url
            url: 文件url,
            success: function (res) {
                _this.filePath = res.tempFilePath;
                wx.shareFileMessage({
                    filePath: _this.filePath,
                    success() {},
                    fail: console.error,
                })
            }
        });
    }
}

官网

下载方法(uniapp)

uni.uploadFile(OBJECT) | uni-app官网

新页面打开文档(uniapp)

uni.saveFile(OBJECT) @savefile | uni-app官网

转发(微信小程序)

wx.shareFileMessage(Object object) | 微信开放文档

注意

  1. 转发分享仅在微信小程序中生效

  2. 查看文件,类型仅支持doc, xls, ppt, pdf, docx, xlsx, pptx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值