upload-file.uvue 2.5 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  <!-- #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="demo">
          <image
            v-if="imageSrc"
            :src="imageSrc"
            class="image"
            mode="widthFix"
          ></image>
          <view v-else class="uni-hello-addfile" @click="chooseImage"
            >+ 选择图片</view
          >
        </view>
      </view>
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
24 25
</template>
<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
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
import UploadTask from 'uts.sdk.modules.DCloudUniNetwork.UploadTask';
export default {
	data() {
		return {
			title: 'uploadFile',
			imageSrc: '',
			task: null as UploadTask | null,
			pageVisible: false
		}
	},
	onLoad() {
		this.pageVisible = true;
	},
	onUnload() {
		this.imageSrc = '';
		this.pageVisible = false;
		uni.hideLoading();
		this.task?.abort();
	},
	methods: {
		chooseImage: function() {
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				sourceType: ['album'],
				success: (res) => {
					console.log('chooseImage success, temp path is', res.tempFilePaths[0])
					var imageSrc = res.tempFilePaths[0]
					uni.showLoading({
						title: '上传中'
					})
					this.task = uni.uploadFile({
						url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
						filePath: imageSrc,
						name: 'file',
						formData: {
							'user': 'test'
						},
						success: (res) => {
							if (this.pageVisible) {
								console.log('uploadImage success, res is:', res)
								uni.hideLoading();
								uni.showToast({
									title: '上传成功',
									icon: 'success',
									duration: 1000
								})
								this.imageSrc = imageSrc
							}
						},
						fail: (err) => {
							if (this.pageVisible) {
								console.log('uploadImage fail', err);
								uni.hideLoading();
								uni.showModal({
									content: err.errMsg,
									showCancel: false
								});
							}
						},
					});
				},
				fail: (err) => {
					console.log('chooseImage fail', err)
				}
			})
92 93
		}
	}
DCloud-WZF's avatar
DCloud-WZF 已提交
94
}
95 96 97
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
98 99 100
.image {
  width: 100%;
}
101

DCloud-WZF's avatar
DCloud-WZF 已提交
102 103 104 105 106 107
.demo {
  background: #fff;
  padding: 50rpx;
  justify-content: center;
  align-items: center;
}
108

DCloud-WZF's avatar
DCloud-WZF 已提交
109 110 111 112 113 114 115 116 117 118
.uni-hello-addfile {
  text-align: center;
  line-height: 300rpx;
  background: #fff;
  padding: 50rpx;
  margin-top: 10px;
  font-size: 38rpx;
  color: #808080;
}
</style>