敬告:此 DEMO 演示为开放测试页面,仅用于开发者快速测试体验应用功能,请严格遵守开发者协议,了解更多。
♦ JS-SDK 引用方式:
♦ 普通网页 script 方式加载:下载最新版 jsBridge-v20241115.zip,请在页面上调用 jsBridge 接口之前引用 jsbridge-mini.js 库;
♦ js module 方式引用:npm install ym-jsbridge 具体请参考 npm package
♦ APP端文件及目录管理;
♦ 提供本地文件/目录/下载/解压/哈希/分享/打开等操作接口;
♦ 本地文件路径规则:fs://{MainDirectory}/YourPath
♦ 仅允许如下两个主目录:
一般文件目录 fs://file/YourPath
缓存文件目录 fs://cache/YourPath
jsBridge.fs.mkdir("fs://file/my_dir/", function(succ, msg){ alert(succ ? "创建成功" : msg); });
jsBridge.fs.exist("fs://file/my_dir/", function(succ, msg){ alert(succ ? "存在" : "不存在"); });
jsBridge.fs.exist("fs://file/my_dir/my.txt", function(succ, msg){ alert(succ ? "存在" : "不存在"); });
• 回调参数 data 中以斜杠 / 结束的是子目录,否则是文件。
jsBridge.fs.list("fs://file/", function(succ, data) { alert("succ:" + succ + "\n" + JSON.stringify(data)); });
• 回调参数 data 中以斜杠 / 结束的是子目录,否则是文件。
jsBridge.fs.list("fs://file/my_dir/", function(succ, data) { alert("succ:" + succ + "\n" + JSON.stringify(data)); });
• 如果文件不存在则返回 0
jsBridge.fs.size("fs://file/my_dir/my.txt", function(succ, data) { alert("succ:" + succ + "\n" + data); });
• 如果指定的是目录,则会删除此目录及其目录下的所有文件。
jsBridge.fs.delete("fs://file/my_dir/", function(succ, msg) { alert(succ ? "删除成功" : msg); });
file:
jsBridge.fs.writeText( //文件路径,不存在则创建,已存在则覆盖 "{{text.file}}", //文本内容(UTF8编码) "Hello world!\n你好,世界!", //回调 function(succ, msg) { alert(succ ? "写入成功" : msg); } );
file:
jsBridge.fs.appendText( //文件路径,不存在则创建,已存在则追加到文件末尾 "{{text.file}}", //文本内容(UTF8编码) "\n这是追加的文本...", //回调 function(succ, msg) { alert(succ ? "追加成功" : msg); } );
file:
jsBridge.fs.readText("{{text.file}}", function(succ, data) { alert("succ:" + succ + "\n" + data); });
file:
jsBridge.fs.writeBinary( //文件路径,不存在则创建,已存在则覆盖 "{{bin.file}}", //二进制的Base64编码串(这是一张 png 图片) "iVBORw0KGgoAAAANSUhEUgAAAEkAAABACAYAAABWfFoUAAAJq0lEQVR4XuWcD4wcdRXHv2/29relFYkIqWL3t4dyt7OloKEoFDAp2NTWFmxatVdAUkT+xNgaKohVG0tArfwRBRptKbaoLUoV+WNqChYu/G8DQdHeztTS3s2ebWML2CK2O3v3e2b2dq+7s7OzO/vn7nr9JZtLZn7v/d7vM2/evPn93hyhzpaMRj6rEU8FoRUKrez8BU4BcND5EXCQmZ2/GzOsPTWpN72zziGxIyYS/aBZpNQsIjqJgZNw9HeAGN3Q0A1Gt1Jqc6K3r7OeMakWYSMmrgBjJpD9nRxEB4OeA/hZDmlrJ+4+0lOtbNfpY2LUr67WCDOYcV61ctl+zPuZtMc05mfiKfu3gWQBBIJkRCOXAepGEE0NOpC7PwPvaMRrVT+tS/Tafy+nLzlBnKWFeKFiupqAD9Q7LoCXQVip99jrq9VVFSTHUNLwLQBXVqs4QL9+AHfrln2LW8aIijtBuBFAKIC+qroy8BRA9yes9JOVBCpCMlojM6B4NYBoBWUZAAcAvAXmAyBy4tIHc/EpXMkQAE+EgKVtlt1lTBBng7AClL2dK7V6x71Tt2zHAco2X0iGjCwG+Gc+8n8FuJMVP+4XHP8pxcR+4ilQ2hQiNZtB48voNAEsY+CHBJzhM+7vAbwK5i16KvNquX7bJ4TP0zSaWUUce0m37AvL6SkLyZDhLQBd4inI3Mka3ZfosR+tdJnd55OtY1o17l/ETIsAVONhgyoItFpBrU5YmdcCjxsTc0nxIr94qlu2Jw/Pg2ZMbGDGAg9D3iXiJfGezJqgRrr7d8XC52iKfgDCjIq6nItCuKkWOG7dZiz8VWa6D8AYj3E36pb9JffxEkhGTHwPjNs8FBjcT59P/Cu9o+KkAnQwouJ2EL7rExHWjFPpxdFeHA6g1r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PGMUGBv0lH2FW9YHUC+gOoaqRHpIIVXwqEe4n5Y5X2b+YwJODmlisbsIPwexW9O4o73buxi/Vm/xkxtySBVAOf8NotPnY+I3wdzh9wX3qIFUEZT3TM1+4gVn9mRebwaIEedJeYPKxqiSvApdUOjw+3cdzQQ3LLdb4YT83uJzj991StNuTXQf6W4miBHrSXnDkhNapmpaaAGDnS8Xnd9rYPyNwc8nUpl1wwUnP+7/AYc+FGCCh4C2AAAAAElFTkSuQmCC", //回调 function(succ, msg) { alert(succ ? "写入成功" : msg); } );
file:
jsBridge.fs.appendBinary( //文件路径,不存在则创建,已存在则追加到文件末尾 "fs://file/my_dir/my.bin", //二进制的Base64编码串 "XG7ov5nmmK/ov73liqDnmoTmlofmnKwuLi4=", //回调 function(succ, msg) { alert(succ ? "追加成功" : msg); } );
file:
jsBridge.fs.readBinary("{{bin.file}}", function(succ, data) { //data参数返回Base64串,请自行转码 alert("succ:" + succ + "\n" + data); });
jsBridge.fs.copy( //源文件 "fs://file/my_dir/my.txt", //复制为目标文件 "fs://file/my_dir/my_copy.txt", //回调 function(succ, msg) { alert(succ ? "复制成功" : msg); } );
• 转换为 file:///... 形式
jsBridge.fs.toUri("fs://file/my_dir/www/index.html", function(succ, data) { alert("succ:" + succ + "\n" + data); });
jsBridge.fs.toAbsolute("fs://file/my_dir/my.txt", function(succ, data) { alert("succ:" + succ + "\n" + data); });
file:
jsBridge.fs.share("{{share1.file}}", function(succ, msg) { if (!succ) { alert(msg); } });
jsBridge.fs.share("fs://file/my_dir/love.png", function(succ, msg) { if (!succ) { alert(msg); } });
jsBridge.fs.open("fs://file/my_dir/my.txt", function(succ, msg) { if (!succ) { alert(msg); } });
• 记录文件下载状态,已下载的可以避免重复下载;
• 支持断点续传;
• 支持下载完成后自动打开文件;
url:
path:
force:
autoOpen:
jsBridge.fs.download({ //GET请求的网络链接 url : "{{download1.url}}", //下载保存到本地文件 path : "{{download1.path}}", //如果文件已经下载过了,是否强制重新下载,默认false (仅安卓) //true 时重新下载文件 //false 时不重新下载 force : {{download1.force}}, //下载完成后是否自动打开文件,默认 false (仅安卓) autoOpen: {{download1.autoOpen}}, //下载进度回调 progress: function(total, loaded) { console.log("共" + total + "字节,已下载" + loaded); } }, //下载结束回调 function(succ, msg) { alert(succ ? "下载成功" : msg); } );
url:
path:
jsBridge.fs.download({ //GET请求的网络链接 url : "{{download2.url}}", //下载保存到本地文件 path : "{{download2.path}}", //true 始终下载,false 如果已经下载过了就不再下载 force : true, //下载进度回调 progress: function(total, loaded) { console.log("共" + total + "字节,已下载" + loaded); } }, //下载结束回调 function(succ, msg) { alert(succ ? "下载成功" : msg); } );
jsBridge.fs.download({ //GET请求的网络链接 url : "https://app.applebyme.cn/doc/doc.pdf", //下载保存到本地文件 path : "fs://file/my_dir/doc.pdf", //下载完成是否自动打开文件,默认 false (仅安卓) autoOpen: true, //下载进度回调 progress: function(total, loaded) { console.log("共" + total + "字节,已下载" + loaded); } }, //下载结束回调 function(succ, msg) { if (!succ) { alert("下载失败:" + msg); } } );
var url = "https://app.applebyme.cn/doc/doc.pdf"; var path = "fs://file/my_dir/doc.pdf"; jsBridge.fs.exist(path, function(succ, msg){ if (succ) { //使用 文档阅读器 插件打开 jsBridge.txDoc.open({ file: path }); } else { alert("本地文件不存在,准备下载。"); jsBridge.fs.download({ //GET请求的网络链接 url : url, //下载保存到本地文件 path : path, //下载进度回调 progress: function(total, loaded) { console.log("共" + total + "字节,已下载" + loaded); } }, //下载结束回调 function(succ, msg) { if (succ) { alert("下载完成,准备打开。"); jsBridge.txDoc.open({ file: path }); } else { alert("下载失败:" + msg); } } ); } });
var url = "http://1252463788.vod2.myqcloud.com/95576ef5vodtransgzp1252463788/e1ab85305285890781763144364/v.f10.mp4"; var path = "fs://file/my_dir/f10.mp4"; jsBridge.fs.exist(path, function(succ, msg){ if (succ) { //使用 超级播放器 插件原生播放本地视频 jsBridge.txVideo.play(path); } else { alert("本地文件不存在,准备下载。"); jsBridge.fs.download({ //GET请求的网络链接 url : url, //下载保存到本地文件 path : path, //下载进度回调 progress: function(total, loaded) { console.log("共" + total + "字节,已下载" + loaded); } }, //下载结束回调 function(succ, msg) { if (succ) { alert("下载完成,准备播放。"); jsBridge.txVideo.play(path); } else { alert("下载失败:" + msg); } } ); } });
jsBridge.fs.download({ //下载链接 url : "https://app.applebyme.cn/doc/www.zip", //保存为 path: "fs://file/my_dir/www.zip" }, function(succ, msg) { if (succ) { alert("下载成功"); //解压 jsBridge.fs.unzip({ //要解压的 zip 文件 src: "fs://file/my_dir/www.zip", //解压到哪个目录 dst: "fs://file/my_dir/", //密码 pwd: "" }, function(succ, msg) { alert(succ ? "解压成功" : msg); }); } else { alert("下载失败" + msg); } });
jsBridge.fs.download({ url : "https://app.applebyme.cn/doc/www-with-password.zip", path: "fs://file/my_dir/www-with-password.zip" }, function(succ, msg) { if (succ) { alert("下载成功"); //解压 jsBridge.fs.unzip({ //要解压的 zip 文件 src: "fs://file/my_dir/www-with-password.zip", //解压到哪个目录 dst: "fs://file/my_dir/", //密码 pwd: "123456" }, function(succ, msg) { alert(succ ? "解压成功" : msg); }); } else { alert("下载失败" + msg); } });
jsBridge.fs.list("fs://file/my_dir/www/", function(succ, data) { alert("succ:" + succ + "\n" + JSON.stringify(data)); });
• 计算文件 MD5 哈希值
jsBridge.fs.md5("fs://file/my_dir/doc.pdf", function(succ, data) { alert("succ:" + succ + "\n" + data); });
• 计算文件 SHA1 哈希值
jsBridge.fs.sha1("fs://file/my_dir/doc.pdf", function(succ, data) { alert("succ:" + succ + "\n" + data); });
• 计算文件 SHA256 哈希值
jsBridge.fs.sha256("fs://file/my_dir/doc.pdf", function(succ, data) { alert("succ:" + succ + "\n" + data); });