download-file.uvue 2.3 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3 4 5 6 7 8 9 10 11
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view v-if="imageSrc" class="image-container">
          <image class="img" :src="imageSrc" mode="center" />
        </view>
        <view v-else style="margin-top: 50px">
Y
yurj26 已提交
12
          <text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
13 14 15 16 17 18 19 20 21
          <view class="uni-btn-v">
            <button type="primary" @tap="downloadImage">下载</button>
          </view>
        </view>
      </view>
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
22 23
</template>
<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
24 25 26 27 28 29
export default {
	data() {
		return {
			title: 'downloadFile',
			imageSrc: '',
			task: null as DownloadTask | null,
30 31 32
			pageVisible: false,
      //自动化测试例专用
      jest_result: false
DCloud-WZF's avatar
DCloud-WZF 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		}
	},
	onLoad() {
		this.pageVisible = true;
	},
	onUnload() {
		this.imageSrc = '';
		this.pageVisible = false;
		uni.hideLoading();
		this.task?.abort();
	},
	methods: {
		downloadImage: function() {
			uni.showLoading({
				title: '下载中'
			})
			var self = this
			this.task = uni.downloadFile({
				url: "https://web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
				success(res) {
					if (this.pageVisible) {
						console.log('downloadFile success, res is', res)
						self.imageSrc = res.tempFilePath;
						uni.hideLoading();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
57
					}
DCloud-WZF's avatar
DCloud-WZF 已提交
58 59
				},
				fail: (err) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
60
					if (this.pageVisible) {
DCloud-WZF's avatar
DCloud-WZF 已提交
61 62
						console.log('downloadFile fail, err is:', err)
						uni.hideLoading();
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
63
					}
DCloud-WZF's avatar
DCloud-WZF 已提交
64 65 66 67 68 69 70
				}
			});
			this.task?.onProgressUpdate((update) => {
				if (this.pageVisible) {
					console.log("progress : ", update.progress);
				}
			})
71 72 73 74 75 76 77 78 79 80 81 82 83
		},
    //自动化测试例专用
    jest_downloadFile(){
      uni.downloadFile({
      	url: "https://web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
      	success() {
          this.jest_result = true
      	},
      	fail: () => {
          this.jest_result = false
      	}
      });
    }
84
	}
DCloud-WZF's avatar
DCloud-WZF 已提交
85
}
86 87 88
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
89 90 91 92 93
.img {
  width: 500rpx;
  height: 500rpx;
  margin: 0 auto;
}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
94

DCloud-WZF's avatar
DCloud-WZF 已提交
95 96 97 98 99 100
.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>