cloud-storage.uvue 2.4 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2
<template>
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
雪洛's avatar
雪洛 已提交
4 5 6 7 8 9
  <!-- #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>
雪洛's avatar
雪洛 已提交
10
          <button type="primary" @click="chooseAndUploadFile">一个接口选择文件并上传</button>
雪洛's avatar
雪洛 已提交
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
        </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
雪洛 已提交
42
              .then(function (res) {
雪洛's avatar
雪洛 已提交
43 44 45 46 47 48 49
                uni.hideLoading()
                console.log(res)
                uni.showModal({
                  content: '上传成功',
                  showCancel: false
                });
              })
雪洛's avatar
雪洛 已提交
50
              .catch(function (err : any | null) {
雪洛's avatar
雪洛 已提交
51 52 53 54 55 56 57
                uni.hideLoading()
                const error = err as UniCloudError
                uni.showModal({
                  content: '上传失败,' + error.errMsg,
                  showCancel: false
                });
              })
58 59 60
            // .finally((_: number) : void => {
            //   uni.hideLoading()
            // })
雪洛's avatar
雪洛 已提交
61 62 63 64 65
          },
          fail(err) : void {
            console.error('chooseImage fail: ', err)
          }
        })
雪洛's avatar
雪洛 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      },
      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
            });
          })
雪洛's avatar
雪洛 已提交
86 87 88 89 90 91
      }
    }
  }
</script>

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