美文网首页
mpvue小程序打开文档

mpvue小程序打开文档

作者: 泪滴在琴上 | 来源:发表于2024-01-23 14:02 被阅读0次

微信小程序中使用 wx.downloadFile 来下载文件,然后可以使用 wx.saveFile 将临时文件保存到本地。
wx.downloadFile 首先下载文件并提供一个临时路径 tempFilePath。然后,wx.saveFile 将这个临时文件保存到本地文件系统。

viewPdfFile(item) {
            var that = this
            var pdfUrl = item.pdfUrl;
            var filename = item.feature;
            wx.downloadFile({
                url: pdfUrl,  //.pdf文件地址或者可以在浏览器直接下载pdf的地址
                success: function (res) {
                    var Path = res.tempFilePath; //返回的文件临时地址,用于后面打开本地预览所用
                    var ext = Path.substring(Path.lastIndexOf('.') + 1)
                     wx.saveFile({
                        tempFilePath: Path,
                        success: function (result) {
                            const savedFilePath = result.savedFilePath;
                            wx.openDocument({
                                filePath: savedFilePath,
                                fileType: 'pdf',// 加上你需要打开的类型不然有些机型打不开报错
                                showMenu: true,
                                success: function (res) {
                                    wx.hideLoading();
                                    // 统计白皮书点击次数
                                    that.$http.post("/minipro/henkel/SetPdfNum", {
                                        pdf_url: pdfUrl,
                                        pdf_name: filename + '.' + ext,
                                    })
                                        .then(res => {
                                            that.pdfNumFlag = false
                                            that.pdfUrl = ''
                                            that.filename = ''
                                        })
                                },
                                fail: function (res) {
                                    that.pdfNumFlag = false
                                    wx.hideLoading();
                                    console.log(res);
                                },
                            })
                        },
                        fail: function (err) {
                        }
                    });
                },
                fail: function (res) {
                }
            })
        },

相关文章

网友评论

      本文标题:mpvue小程序打开文档

      本文链接:https://www.haomeiwen.com/subject/nfouodtx.html