cloud-storage.uvue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-padding-wrap uni-common-mt">
  8. <view class="uni-btn-v uni-common-mt">
  9. <button type="primary" @click="uploadFile">选择文件上传</button>
  10. <button type="primary" @click="chooseAndUploadFile">一个接口选择文件并上传</button>
  11. </view>
  12. </view>
  13. </view>
  14. <!-- #ifdef APP -->
  15. </scroll-view>
  16. <!-- #endif -->
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. title: '云存储'
  23. }
  24. },
  25. onLoad() {
  26. },
  27. onUnload() {
  28. },
  29. methods: {
  30. uploadFile: function () {
  31. uni.chooseImage({
  32. count: 1,
  33. success(res) : void {
  34. uni.showLoading({
  35. title: '上传中...'
  36. })
  37. const tempFilePath = res.tempFilePaths[0]
  38. uniCloud.uploadFile({
  39. filePath: tempFilePath,
  40. cloudPath: 'test.jpg'
  41. })
  42. .then(function (res) {
  43. uni.hideLoading()
  44. console.log(res)
  45. uni.showModal({
  46. content: '上传成功',
  47. showCancel: false
  48. });
  49. })
  50. .catch(function (err : any | null) {
  51. uni.hideLoading()
  52. const error = err as UniCloudError
  53. uni.showModal({
  54. content: '上传失败,' + error.errMsg,
  55. showCancel: false
  56. });
  57. })
  58. // .finally((_: number) : void => {
  59. // uni.hideLoading()
  60. // })
  61. },
  62. fail(err) : void {
  63. console.error('chooseImage fail: ', err)
  64. }
  65. })
  66. },
  67. chooseAndUploadFile() {
  68. uniCloud.chooseAndUploadFile({
  69. type: 'image'
  70. }).then(function (res) {
  71. uni.hideLoading()
  72. console.log(res)
  73. uni.showModal({
  74. content: '上传成功',
  75. showCancel: false
  76. });
  77. })
  78. .catch(function (err : any | null) {
  79. uni.hideLoading()
  80. const error = err as UniCloudError
  81. uni.showModal({
  82. content: '上传失败,' + error.errMsg,
  83. showCancel: false
  84. });
  85. })
  86. }
  87. }
  88. }
  89. </script>
  90. <style>
  91. </style>