是是非非
是是非非
  • 发布:2019-08-23 19:11
  • 更新:2020-07-03 10:14
  • 阅读:1090

开发模式下真机运行能读取本地文件,用自有证书云打包之后不能读取文件。

分类:HTML5+

目录接口
项目/html/index/index下面有四个文件
index.html
index.css
index.js
index.json


index.js 在 plusready之后,读取本地的JSON文件,_www/html/index/index/index.json
代码如下:

    /**  
     * 加载JSON文件  
     * @param {Object} url      JSON文件地址  
     * @param {Object} success  成功处理  
     * @param {Object} fail     失败处理  
     * @param {Object} complete 无论如何都处理  
     */  
    loadJSON: function(url, success, fail, complete) {  
        console.warn('loadJSON : ' + url);  
        if (url.charAt(0) === '/')  
            url = url.substring(1);  
        console.warn('loadJSON : ->' + url);  
        plus.io.requestFileSystem(plus.io.PRIVATE_WWW, function(fs) {  
            fs.root.getFile(url, {  
                create: false  
            }, function(f) {  
                console.warn('loadJSON : ' + f.fullPath);  
                var rdr = new plus.io.FileReader();  
                rdr.onerror = function(e) {  
                    console.error('读取文件失败:' + typeof(e));  
                    console.error('读取文件失败:' + JSON.stringify(e));  

                    fail.call(App, null, null, {  
                        __code: 'ERROR',  
                        __message: '读取文件失败'  
                    });  
                    complete.call(App);  
                };  
                rdr.onload = function(e) {  
                    var obj = null;  
                    try {  
                        obj = JSON.parse(e.target.result);  
                    } catch (ex) {  
                        fail.call(App, null, null, {  
                            __code: 'ERROR',  
                            __message: ex.message  
                        });  
                        complete.call(App);  
                        return;  
                    }  
                    success.call(App, null, null, {  
                        __code: 'OK',  
                        __message: '',  
                        data: obj  
                    });  
                    complete.call(App);  
                };  
                rdr.readAsText(f, 'utf-8');  
            }, function(e) {  
                console.error('打开文件失败:' + typeof(e));  
                console.error('读取文件失败:' + JSON.stringify(e));  
                fail.call(App, null, null, {  
                    __code: 'ERROR',  
                    __message: '打开文件失败'  
                });  
                complete.call(App);  
            });  
        }, function(e) {  
            console.error('请求文件系统失败:' + typeof(e));  
            console.error('请求文件系统失败:' + JSON.stringify(e));  
            fail.call(App, null, null, {  
                __code: 'ERROR',  
                __message: '请求文件系统失败'  
            });  
            complete.call(App);  
        });  
    },

出错在 打开文件失败,{"code":10,"message":"执行出错"}

2019-08-23 19:11 负责人:无 分享
已邀请:
是是非非

是是非非 (作者)

直接真机运行调试,日志如下:

19:14:08.878 loadJSON : /html/index/index/index.json at js/app.js:428
19:14:08.899 loadJSON : ->html/index/index/index.json at js/app.js:431
19:14:08.980 loadJSON : /storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/www/html/index/index/index.json at js/app.js:436

是是非非

是是非非 (作者)

打包运行错误日志如下:
19:45:36.190 loadJSON : /html/index/index/index.json at js/app.js:428
19:45:36.210 loadJSON : ->html/index/index/index.json at js/app.js:431
19:45:36.271 打开文件失败:object at js/app.js:469
19:45:36.291 读取文件失败:{"code":10,"message":"执行出错"} at js/app.js:470
19:45:36.312 同步#2 : 加载页面配置失败:打开文件失败 at js/app.js:1681

大哥要不要

大哥要不要

mark一下,可能是这个原因

应用运行资源目录"_www",仅本应用可访问。 为了确保应用资源的安全性,此目录只可读。
注意: 需要将应用设置为释放资源模式才能访问此目录,配置方法:
uni-app项目,在manifest.json的"app-plus"节点下添加"runmode":"liberate"
5+ App项目,在manifest.json的"plus"节点下添加"runmode":"liberate"

runmode可取值:

"normal"
正常运行(不释放资源)模式,直接使用应用的资源,无法直接使用File API(plus.io.*)访问应用资源。
"liberate"
释放资源运行模式,应用在第一次启动时将解压应用私有目录。该模式的缺点:第一次启动更慢,耗费时间先解压。该模式有用的场景:此模式下File API才可正常访问_www应用资源,以及在某些Android rom访问本地页面时url地址中包含?带参数,但不推荐使用这种跨页传参方式,推荐使用其他方式跨页传参http://ask.dcloud.net.cn/article/288。
注意:wap2app 应用固定为 liberate,不支持自定义。

该问题目前已经被锁定, 无法添加新回复