FileSystemManager

解释:文件管理器。

  • 在 swan 文件中
  1. <view class="wrap">
  2. <button type="primary" bindtap="access">access</button>
  3. <button type="primary" bindtap="accessSync">accessSync</button>
  4. <button type="primary" bindtap="appendFile">appendFile</button>
  5. <button type="primary" bindtap="appendFileSync">appendFileSync</button>
  6. <button type="primary" bindtap="copyFile">copyFile</button>
  7. <button type="primary" bindtap="copyFileSync">copyFileSync</button>
  8. <button type="primary" bindtap="getFileInfo">getFileInfo</button>
  9. <button type="primary" bindtap="getSavedFileList">getSavedFileList</button>
  10. <button type="primary" bindtap="mkdir">mkdir</button>
  11. <button type="primary" bindtap="mkdirSync">mkdirSync</button>
  12. <button type="primary" bindtap="readdir">readdir</button>
  13. <button type="primary" bindtap="readdirSync">readdirSync</button>
  14. <button type="primary" bindtap="readFile">readFile</button>
  15. <button type="primary" bindtap="readFileSync">readFileSync</button>
  16. <button type="primary" bindtap="removeSavedFile">removeSavedFile</button>
  17. <button type="primary" bindtap="rename">rename</button>
  18. <button type="primary" bindtap="renameSync">renameSync</button>
  19. <button type="primary" bindtap="rmdir">rmdir</button>
  20. <button type="primary" bindtap="rmdirSync">rmdirSync</button>
  21. <button type="primary" bindtap="saveFile">saveFile</button>
  22. <button type="primary" bindtap="saveFileSync">saveFileSync</button>
  23. <button type="primary" bindtap="stat">stat</button>
  24. <button type="primary" bindtap="statSync">statSync</button>
  25. <button type="primary" bindtap="unlink">unlink</button>
  26. <button type="primary" bindtap="unlinkSync">unlinkSync</button>
  27. <button type="primary" bindtap="unzip">unzip</button>
  28. <button type="primary" bindtap="writeFile">writeFile</button>
  29. <button type="primary" bindtap="writeFileSync">writeFileSync</button>
  30. </view>
  • 在 js 文件中
  1. Page({
  2. data: {},
  3. onLoad() {
  4. const fs = swan.getFileSystemManager();
  5. this.fs = fs;
  6. },
  7. access() {
  8. this.fs.access({
  9. path: `${swan.env.USER_DATA_PATH}/demo.txt`,
  10. success: res => {
  11. console.log('access success', res);
  12. },
  13. fail: err => {
  14. console.log('access fail', err)
  15. }
  16. });
  17. },
  18. accessSync() {
  19. try {
  20. let result = this.fs.accessSync(
  21. `${swan.env.USER_DATA_PATH}/demo.txt`
  22. );
  23. console.log('accessSync success', result);
  24. }
  25. catch (err) {
  26. console.log('accessSync fail', err);
  27. }
  28. },
  29. appendFile() {
  30. this.fs.appendFile({
  31. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  32. data: 'appendFile',
  33. success: res => {
  34. console.log('appendFile success', res);
  35. },
  36. fail: err => {
  37. console.log('appendFile fail', err)
  38. }
  39. });
  40. },
  41. appendFileSync() {
  42. try {
  43. let result = this.fs.appendFileSync(
  44. `${swan.env.USER_DATA_PATH}/demo.txt`,
  45. 'appendFileSync'
  46. );
  47. console.log('appendFileSync success', result);
  48. }
  49. catch (err) {
  50. console.log('appendFileSync fail', err);
  51. }
  52. },
  53. copyFile() {
  54. this.fs.copyFile({
  55. srcPath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  56. destPath: `${swan.env.USER_DATA_PATH}/destPath.txt`,
  57. success: res => {
  58. console.log('copyFile success', res);
  59. },
  60. fail: err => {
  61. console.log('copyFile fail', err)
  62. }
  63. });
  64. },
  65. copyFileSync() {
  66. try {
  67. let result = this.fs.copyFileSync(
  68. `${swan.env.USER_DATA_PATH}/demo.txt`,
  69. `${swan.env.USER_DATA_PATH}/copyFileSync.txt`
  70. );
  71. console.log('copyFileSync success', result);
  72. }
  73. catch (err) {
  74. console.log('copyFileSync fail', err);
  75. }
  76. },
  77. getFileInfo() {
  78. this.fs.getFileInfo({
  79. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  80. success: res => {
  81. console.log('getFileInfo success', res);
  82. },
  83. fail: err => {
  84. console.log('getFileInfo fail', err)
  85. }
  86. });
  87. },
  88. getSavedFileList() {
  89. this.fs.getSavedFileList({
  90. success: res => {
  91. console.log('getSavedFileList success', res);
  92. },
  93. fail: err => {
  94. console.log('getSavedFileList fail', err)
  95. }
  96. });
  97. },
  98. mkdir() {
  99. this.fs.mkdir({
  100. dirPath: `${swan.env.USER_DATA_PATH}/mkdir`,
  101. success: res => {
  102. console.log('mkdir success', res);
  103. },
  104. fail: err => {
  105. console.log('mkdir fail', err)
  106. }
  107. });
  108. },
  109. mkdirSync() {
  110. try {
  111. let result = this.fs.mkdirSync(
  112. `${swan.env.USER_DATA_PATH}/mkdirSyc`
  113. );
  114. console.log('mkdirSync success', result);
  115. }
  116. catch (err) {
  117. console.log('mkdirSync fail', err);
  118. }
  119. },
  120. readdir() {
  121. this.fs.readdir({
  122. dirPath: `${swan.env.USER_DATA_PATH}/readdir`,
  123. success: res => {
  124. console.log('readdir success', res);
  125. },
  126. fail: err => {
  127. console.log('readdir fail', err)
  128. }
  129. });
  130. },
  131. readdirSync() {
  132. try {
  133. let result = this.fs.readdirSync(
  134. `${swan.env.USER_DATA_PATH}/readdirSyc'`
  135. );
  136. console.log('readdirSync success', result);
  137. }
  138. catch (err) {
  139. console.log('readdirSync fail', err);
  140. }
  141. },
  142. readFile() {
  143. this.fs.readFile({
  144. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  145. encoding: 'utf8',
  146. success: res => {
  147. console.log('readFile success', res);
  148. },
  149. fail: err => {
  150. console.log('readFile fail', err)
  151. }
  152. });
  153. },
  154. readFileSync() {
  155. try {
  156. let result = this.fs.readFileSync(
  157. `${swan.env.USER_DATA_PATH}/demo.txt`
  158. );
  159. console.log('readFileSync success', result);
  160. }
  161. catch (err) {
  162. console.log('readFileSync fail', err);
  163. }
  164. },
  165. removeSavedFile() {
  166. this.fs.removeSavedFile({
  167. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  168. encoding: 'utf8',
  169. success: res => {
  170. console.log('removeSavedFile success', res);
  171. },
  172. fail: err => {
  173. console.log('removeSavedFile fail', err)
  174. }
  175. });
  176. },
  177. rename() {
  178. this.fs.rename({
  179. oldPath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  180. newPath: `${swan.env.USER_DATA_PATH}/rename/demo.txt`,
  181. encoding: 'utf8',
  182. success: res => {
  183. console.log('rename success', res);
  184. },
  185. fail: err => {
  186. console.log('rename fail', err)
  187. }
  188. });
  189. },
  190. renameSync() {
  191. try {
  192. let result = this.fs.renameSync(
  193. `${swan.env.USER_DATA_PATH}/demo.txt`,
  194. `${swan.env.USER_DATA_PATH}/renameSync/demo.txt`
  195. );
  196. console.log('renameSync success', result);
  197. }
  198. catch (err) {
  199. console.log('renameSync fail', err);
  200. }
  201. },
  202. rmdir() {
  203. this.fs.rmdir({
  204. dirPath: `${swan.env.USER_DATA_PATH}/rmdir`,
  205. success: res => {
  206. console.log('rmdir success', res);
  207. },
  208. fail: err => {
  209. console.log('rmdir fail', err)
  210. }
  211. });
  212. },
  213. rmdirSync() {
  214. try {
  215. let result = this.fs.rmdirSync(
  216. `${swan.env.USER_DATA_PATH}/rmdirSync`
  217. );
  218. console.log('rmdirSync success', result);
  219. }
  220. catch (err) {
  221. console.log('rmdirSync fail', err);
  222. }
  223. },
  224. saveFile() {
  225. this.fs.saveFile({
  226. tempFilePath: '/usr/temp.txt', // 仅为示例,实际上请传真实临时路径地址,如 swan.downloadFile 的 tempFilePath 返回参数
  227. filePath: `${swan.env.USER_DATA_PATH}/`,
  228. success: res => {
  229. console.log('saveFile success', res);
  230. },
  231. fail: err => {
  232. console.log('saveFile fail', err)
  233. }
  234. });
  235. },
  236. saveFileSync() {
  237. try {
  238. let result = this.fs.saveFileSync(
  239. '/usr/temp.txt', // 仅为示例,实际上请传真实临时路径地址,如 swan.downloadFile 的 tempFilePath 返回参数
  240. `${swan.env.USER_DATA_PATH}/`
  241. );
  242. console.log('saveFileSync success', result);
  243. }
  244. catch (err) {
  245. console.log('saveFileSync fail', err);
  246. }
  247. },
  248. stat() {
  249. this.fs.stat({
  250. path: `${swan.env.USER_DATA_PATH}/demo.txt`,
  251. success: res => {
  252. console.log('stat success', res);
  253. },
  254. fail: err => {
  255. console.log('stat fail', err)
  256. }
  257. });
  258. },
  259. statSync() {
  260. try {
  261. let result = this.fs.statSync(
  262. `${swan.env.USER_DATA_PATH}/demo.txt`
  263. );
  264. console.log('statSync success', result);
  265. }
  266. catch (err) {
  267. console.log('statSync fail', err);
  268. }
  269. },
  270. unlink() {
  271. this.fs.unlink({
  272. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  273. success: res => {
  274. console.log('unlink success', res);
  275. },
  276. fail: err => {
  277. console.log('unlink fail', err)
  278. }
  279. });
  280. },
  281. unlinkSync() {
  282. try {
  283. let result = this.fs.unlinkSync(
  284. `${swan.env.USER_DATA_PATH}/demo.txt`
  285. );
  286. console.log('unlinkSync success', result);
  287. }
  288. catch (err) {
  289. console.log('unlinkSync fail', err);
  290. }
  291. },
  292. unzip() {
  293. this.fs.unzip({
  294. zipFilePath: `${swan.env.USER_DATA_PATH}/demo/a.zip`,
  295. targetPath: `${swan.env.USER_DATA_PATH}/demo/b`,
  296. success: res => {
  297. console.log('unzip success', res);
  298. },
  299. fail: err => {
  300. console.log('unzip fail', err)
  301. }
  302. });
  303. },
  304. writeFile() {
  305. this.fs.writeFile({
  306. filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
  307. data: 'writeFile',
  308. success: res => {
  309. console.log('writeFile success', res);
  310. },
  311. fail: err => {
  312. console.log('writeFile fail', err)
  313. }
  314. });
  315. },
  316. writeFileSync() {
  317. try {
  318. let result = this.fs.writeFileSync(
  319. `${swan.env.USER_DATA_PATH}/demo.txt`,
  320. 'writeFileSync'
  321. );
  322. console.log('writeFileSync success', result);
  323. }
  324. catch (err) {
  325. console.log('writeFileSync fail', err);
  326. }
  327. }
  328. });