file.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '文件',
  5. path: 'page/API/pages/file/file'
  6. }
  7. },
  8. onLoad() {
  9. this.setData({
  10. savedFilePath: wx.getStorageSync('savedFilePath')
  11. })
  12. },
  13. data: {
  14. tempFilePath: '',
  15. savedFilePath: '',
  16. dialog: {
  17. hidden: true
  18. }
  19. },
  20. chooseImage() {
  21. const that = this
  22. wx.chooseImage({
  23. count: 1,
  24. success(res) {
  25. that.setData({
  26. tempFilePath: res.tempFilePaths[0]
  27. })
  28. }
  29. })
  30. },
  31. saveFile() {
  32. if (this.data.tempFilePath.length > 0) {
  33. const that = this
  34. wx.saveFile({
  35. tempFilePath: this.data.tempFilePath,
  36. success(res) {
  37. that.setData({
  38. savedFilePath: res.savedFilePath
  39. })
  40. wx.setStorageSync('savedFilePath', res.savedFilePath)
  41. that.setData({
  42. dialog: {
  43. title: '保存成功',
  44. content: '下次进入应用时,此文件仍可用',
  45. hidden: false
  46. }
  47. })
  48. },
  49. fail() {
  50. that.setData({
  51. dialog: {
  52. title: '保存失败',
  53. content: '应该是有 bug 吧',
  54. hidden: false
  55. }
  56. })
  57. }
  58. })
  59. }
  60. },
  61. clear() {
  62. wx.setStorageSync('savedFilePath', '')
  63. this.setData({
  64. tempFilePath: '',
  65. savedFilePath: ''
  66. })
  67. },
  68. confirm() {
  69. this.setData({
  70. 'dialog.hidden': true
  71. })
  72. }
  73. })