收藏
回答

真机测试和体验版都可以正常下载打开.docx文件,但是线上版本却不行?

downloadAnnex(url){

        let _this = this;

        let fileName = url.split('/');

        let downloadTask = wx.downloadFile({

            url: api.download+url,

            success: function (res) {

                // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容

                var filePath = res.tempFilePath;

                let manager = wx.getFileSystemManager();  //获取全局唯一的文件管理器

                //判断目录是否存在

                manager.access({

                    path: `${wx.env.USER_DATA_PATH}/download`,

                    //存在目录则查看是否存在此文件  

                    success: (res=> {

                        manager.access({

                            path: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                            // 存在文件则展示

                            success(res) {

                                Taro.openDocument({

                                    filePath: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                                    fileType: 'docx',

                                    success:(res)=>{

                                        Taro.showToast({

                                            title: '正在打开',

                                            icon: 'success',

                                            duration: 2000

                                          })

                                    },

                                    fail:(err)=>{

                                        Taro.showToast({

                                            title: '打开失败',

                                            duration: 2000

                                        })

                                      console.log('读取失败',err)

                                    }

                                  })

                                return false;


                            },

                            // 不存在文件则把缓存文件用saveFile另存为

                            fail(err) {

                                manager.saveFile({

                                    tempFilePath: filePath,     //filePath为保存到本地的临时路径

                                    filePath: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                                    success: (res=> {

                                        Taro.openDocument({

                                            filePath: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                                            fileType: 'docx',

                                            success:(res)=>{

                                                Taro.showToast({

                                                    title: '下载成功',

                                                    icon: 'success',

                                                    duration: 2000

                                                  })

                                            },

                                            fail:(err)=>{

                                                Taro.showToast({

                                                    title: '下载失败',

                                                    duration: 2000

                                                })

                                            }

                                          })

                                    },

                                    fail: (err=> {

                                        console.log(err)

                                    }

                                })

                            }

                        })

                    },

                    //目录不存在则创建目录

                    fail(err){

                        manager.mkdir({

                            dirPath: `${wx.env.USER_DATA_PATH}/download`,

                            recursive: false//是否在递归创建该目录的上级目录后再创建该目录。

                            success: (res=> {

                                //创建目录成功后将临时文件保存到目录  /download

                                manager.saveFile({

                                    tempFilePath: filePath,

                                    filePath: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                                    success: (res=> {

                                        Taro.openDocument({

                                            filePath: `${wx.env.USER_DATA_PATH}/download/${fileName[2]}`,

                                            fileType: 'docx',

                                            success:(res)=>{

                                              console.log('读取成功',res)

                                            },

                                            fail:(err)=>{

                                              console.log('读取失败',err)

                                            }

                                          })

                                    },

                                    fail: (err=> {

                                        console.log(err)

                                      }

                                })

                            }

                        })

                    }

                })

            },

            fail: (err)=>{

                console.log(err"下载失败")

            }

        });

    }


最后一次编辑于  09-25
回答关注问题邀请回答
收藏

1 个回答

问题标签