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

<style>
雪洛's avatar
雪洛 已提交
92
</style>