WebApp快捷打包
FileSystem 文件系统

JS-SDK,下载最新版 jsBridge-v20200917.zip,请在页面上调用 jsBridge 接口之前引用 jsbridge-mini.js 库。

♦ APP端文件及目录管理;

♦ 提供本地文件/目录/下载/解压/哈希/分享/打开等操作接口;

♦ 本地文件路径规则:fs://{MainDirectory}/YourPath

仅允许如下两个主目录:
一般文件目录 fs://file/YourPath
缓存文件目录 fs://cache/YourPath

mkdir 创建目录

jsBridge.fs.mkdir("fs://file/my_dir/", function(succ, msg){
  alert(succ ? "创建成功" : msg);
});

exist 文件或目录是否存在:示例一

jsBridge.fs.exist("fs://file/my_dir/", function(succ, msg){
  alert(succ ? "存在" : "不存在");
});

exist 文件或目录是否存在:示例二

jsBridge.fs.exist("fs://file/my_dir/my.txt", function(succ, msg){
  alert(succ ? "存在" : "不存在");
});

list 列出目录下的文件和子目录:示例一

• 回调参数 data 中以斜杠 / 结束的是子目录,否则是文件。

jsBridge.fs.list("fs://file/", function(succ, data) {
  alert("succ:" + succ + "\n" + JSON.stringify(data));
});

list 列出目录下的文件和子目录:示例二

• 回调参数 data 中以斜杠 / 结束的是子目录,否则是文件。

jsBridge.fs.list("fs://file/my_dir/", function(succ, data) {
  alert("succ:" + succ + "\n" + JSON.stringify(data));
});

size 获取文件大小(byte)

• 如果文件不存在则返回 0

jsBridge.fs.size("fs://file/my_dir/my.txt", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

delete 删除目录或文件

• 如果指定的是目录,则会删除此目录及其目录下的所有文件。

jsBridge.fs.delete("fs://file/my_dir/", function(succ, msg) {
  alert(succ ? "删除成功" : msg);
});

writeText 写入 UTF8 文本内容

jsBridge.fs.writeText(
  //文件路径,不存在则创建,已存在则覆盖
  "fs://file/my_dir/my.txt",
  //文本内容(UTF8编码)
  "Hello world!\n你好,世界!",
  //回调
  function(succ, msg) {
    alert(succ ? "写入成功" : msg);
  }
);

appendText 追加 UTF8 文本内容

jsBridge.fs.appendText(
  //文件路径,不存在则创建,已存在则追加到文件末尾
  "fs://file/my_dir/my.txt",
  //文本内容(UTF8编码)
  "\n这是追加的文本...",
  //回调
  function(succ, msg) {
    alert(succ ? "追加成功" : msg);
  }
);

readText 读取文本文件

jsBridge.fs.readText("fs://file/my_dir/my.txt", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

writeBinary 写入二进制内容

jsBridge.fs.writeBinary(
  //文件路径,不存在则创建,已存在则覆盖
  "fs://file/my_dir/love.png",
  //二进制的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/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UU3/o1CEWNQatSljpG4KC8vGgg6zUnGYAajokP1AArdGt9LXVgvIB9DYrNa9ZgIYEki8owlq9xy4savVkVg4Qgf+tFM9vJqAhg+TvUfi1btnujcFBWD4etFdj1dGe6nuuWm+stV/DUwA/QwwpPGMUGBv0lH2FW9YHUC+gOoaqRHpIIVXwqEe4n5Y5X2b+YwJODmlisbsIPwexW9O4o73buxi/Vm/xkxtySBVAOf8NotPnY+I3wdzh9wX3qIFUEZT3TM1+4gVn9mRebwaIEedJeYPKxqiSvApdUOjw+3cdzQQ3LLdb4YT83uJzj991StNuTXQf6W4miBHrSXnDkhNapmpaaAGDnS8Xnd9rYPyNwc8nUpl1wwUnP+7/AYc+FGCCh4C2AAAAAElFTkSuQmCC",
  //回调
  function(succ, msg) {
    alert(succ ? "写入成功" : msg);
  }
);

appendBinary 追加二进制内容

jsBridge.fs.appendBinary(
  //文件路径,不存在则创建,已存在则追加到文件末尾
  "fs://file/my_dir/my.bin",
  //二进制的Base64编码串
  "XG7ov5nmmK/ov73liqDnmoTmlofmnKwuLi4=",
  //回调
  function(succ, msg) {
    alert(succ ? "追加成功" : msg);
  }
);

readBinary 读取二进制文件

jsBridge.fs.readBinary("fs://file/my_dir/my.bin", function(succ, data) {
  //data参数返回Base64串,请自行转码
  alert("succ:" + succ + "\n" + data);
});

copy 复制文件

jsBridge.fs.copy(
  //源文件
  "fs://file/my_dir/my.txt",
  //复制为目标文件
  "fs://file/my_dir/my_copy.txt",
  //回调
  function(succ, msg) {
    alert(succ ? "复制成功" : msg);
  }
);

toUri 转换文件路径为本地 URI 路径

• 转换为 file:///... 形式

jsBridge.fs.toUri("fs://file/my_dir/www/index.html", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

toAbsolute 转换文件路径为本地绝对路径

jsBridge.fs.toAbsolute("fs://file/my_dir/my.txt", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

share 分享文件(调用系统分享功能):示例一

jsBridge.fs.share("fs://file/my_dir/my.txt", function(succ, msg) {
  if (!succ) {
    alert(msg);
  }
});

share 分享文件(调用系统分享功能):示例二

jsBridge.fs.share("fs://file/my_dir/love.png", function(succ, msg) {
  if (!succ) {
    alert(msg);
  }
});

open 打开文件(调用系统打开功能)

jsBridge.fs.open("fs://file/my_dir/my.txt", function(succ, msg) {
  if (!succ) {
    alert(msg);
  }
});

download 下载文件:示例一

jsBridge.fs.download({
  //GET请求的网络链接
  url     : "https://www.yimenapp.com/doc/doc.pdf",
  //下载保存到本地文件
  path    : "fs://file/my_dir/doc.pdf",
  //下载进度回调
  progress: function(total, loaded) {
      console.log("共" + total + "字节,已下载" + loaded);
    }
  },
  //下载结束回调
  function(succ, msg) {
    alert(succ ? "下载成功" : msg);
  }
);

download 下载文件:示例二

jsBridge.fs.download({
  //GET请求的网络链接
  url     : "https://www.yimenapp.com/doc/www.zip",
  //下载保存到本地文件
  path    : "fs://file/my_dir/www.zip",
  //下载进度回调
  progress: function(total, loaded) {
      console.log("共" + total + "字节,已下载" + loaded);
    }
  },
  //下载结束回调
  function(succ, msg) {
    alert(succ ? "下载成功" : msg);
  }
);

unzip 解压 zip 文件

//请先执行上一步,下载 www.zip 文件
jsBridge.fs.unzip(
  //要解压的 zip 文件
  "fs://file/my_dir/www.zip",
  //解压到哪个目录
  "fs://file/my_dir/",
  //回调
  function(succ, msg) {
   alert(succ ? "解压成功" : msg);
  }
);

list 查看解压出来的目录

jsBridge.fs.list("fs://file/my_dir/www/", function(succ, data) {
  alert("succ:" + succ + "\n" + JSON.stringify(data));
});

md5 MD5 哈希值

• 计算文件 MD5 哈希值

jsBridge.fs.md5("fs://file/my_dir/doc.pdf", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

sha1 SHA1 哈希值

• 计算文件 SHA1 哈希值

jsBridge.fs.sha1("fs://file/my_dir/doc.pdf", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});

sha256 SHA256 哈希值

• 计算文件 SHA256 哈希值

jsBridge.fs.sha256("fs://file/my_dir/doc.pdf", function(succ, data) {
  alert("succ:" + succ + "\n" + data);
});