unicloud-file-api.uvue 1.7 KB
Newer Older
雪洛's avatar
雪洛 已提交
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
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #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>
        </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'
            })
雪洛's avatar
雪洛 已提交
41
              .then(function (res) {
雪洛's avatar
雪洛 已提交
42 43 44 45 46 47 48
                uni.hideLoading()
                console.log(res)
                uni.showModal({
                  content: '上传成功',
                  showCancel: false
                });
              })
雪洛's avatar
雪洛 已提交
49
              .catch(function (err : any | null) {
雪洛's avatar
雪洛 已提交
50 51 52 53 54 55 56
                uni.hideLoading()
                const error = err as UniCloudError
                uni.showModal({
                  content: '上传失败,' + error.errMsg,
                  showCancel: false
                });
              })
57 58 59
            // .finally((_: number) : void => {
            //   uni.hideLoading()
            // })
雪洛's avatar
雪洛 已提交
60 61 62 63 64 65 66 67 68 69 70
          },
          fail(err) : void {
            console.error('chooseImage fail: ', err)
          }
        })
      }
    }
  }
</script>

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