downloadFile存到小程序本地缓存,iOS加载不出图片,安卓可以加载出来
发布于 5 年前 作者 chao62 17420 次浏览 来自 问答

//在onLaunch页面下载zip文件包,并存到本地,在对zip包进行解压(zip包里面是图片),那么所有图片以及zip包都存在了本地缓存的根目录。


代码如下:

onLaunch: function(obj) {

this.downloadFileFn();

},

downloadFileFn: function() {

    let url = wx.env.USER_DATA_PATH;

    wx.setStorageSync(‘USER_DATA_PATH’, url);

    const FileSystemManager = wx.getFileSystemManager();   

 

    FileSystemManager.stat({

        path: url, //文件/目录路径

        recursive: true,

        success: function (res) {

            console.log(’*******stat-成功-判断zip是否存在’, url)

            let isFileSystem = “N”;

            let list = res.stats;

            for (let index in list) {

                let path = list[index].path;

                if (path == ‘fileSystem.zip’) {

                    isFileSystem = “Y”;

                }

            }

            if (isFileSystem == “N”) {

                console.log(’*******zip不存在,开始下载zip’, url)

                wx.downloadFile({

                    url: https://my.chinasie.com/attcenter/api/sys/fileSystem.zip,

                    header: {

                    ‘content-type’: ‘application/json’,

                    “Authorization”: "Bearer "

                    },

                    success(res) {

                        console.log(’*******downloadFile-成功-开始保存到本地’, res)

                        if (res.statusCode === 200) {

                            FileSystemManager.saveFile({

                                tempFilePath: res.tempFilePath,

                                filePath: url + “/fileSystem.zip”,

                                success: function (res) {

                                console.log(’*******saveFile-保存成功-开始解压’, res)

                                

                                FileSystemManager.unzip({

                                    zipFilePath: `${url}/fileSystem.zip`,

                                    targetPath: `${wx.env.USER_DATA_PATH}`,

                                    success: function (res) {

                                    console.log(’-------------unzip-成功’, res)

 

                                    FileSystemManager.stat({

                                    path: url, //文件/目录路径

                                    recursive: true,

                                    success: function (res) {

                                    debugger

                                    console.log(’*******stat-解压成功查看所有结果集’, res)

                                    },

                                    fail: function (ex) {

                                    console.log(’*******stat-失败’, ex)

                                    }

                                    })

                    },

                    fail: function (ex) {

                    console.log(’-------------unzip-失败’, ex)

                    }

                    })

        },

    fail: function (ex) {

    console.log(’*******saveFile-失败’, ex)

    }

    })

}

}

})

} else {

console.log(’*******zip已存在’)

}

}

})

},

page页面使用如下:

获取URL,在跟在解压的图片文件名,


page.js

let url = wx.env.USER_DATA_PATH;;

this.setData({

USER_DATA_PATH:url

})

page.wxml

<image src=’{{USER_DATA_PATH+"/agree.png"}}’></image>  图片名非真实的

 <image src=’{{USER_DATA_PATH+"/login-banner1.jpg"}}’></image>

 <image src=’{{USER_DATA_PATH+"/department.png"}}’></image>


如图:

iOS  图片读取不出来  




如图:

安卓手机  图片可以读取出来  

能否知情中尽快回复,比较着急,非常感谢。

1 回复

你好,请提供一下出现问题的机型和微信版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

回到顶部