Blame view

pages/API/download-file/download-file.vue 1.31 KB
4b045f7c   刘淇   江阴初始化项目
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
  <template>
  	<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>
  			<block v-else style="margin-top: 50px;">
  				<view class="uni-hello-text">
  					点击按钮下载服务端示例图片(下载网络文件到本地临时目录)
  				</view>
  				<view class="uni-btn-v">
  					<button type="primary" @tap="downloadImage">下载</button>
  				</view>
  			</block>
  		</view>
  	</view>
  </template>
  <script>
  	export default {
  		data() {
  			return {
  				title: 'downloadFile',
  				imageSrc: ''
  			}
  		},
  		onUnload() {
  			this.imageSrc = '';
  		},
  		methods: {
  			downloadImage: function () {
  				uni.showLoading({
  					title:'下载中'
  				})
  				var self = this
  				uni.downloadFile({
  					url: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  					success: (res) => {
  						console.log('downloadFile success, res is', res)
  						self.imageSrc = res.tempFilePath;
  						uni.hideLoading();
  					},
  					fail: (err) => {
  						console.log('downloadFile fail, err is:', err)
  					}
  				})
  			}
  		}
  	}
  </script>
  
  <style>
  .img {
  	width: 500rpx;
  	height: 500rpx;
  	margin: 0 auto;
  }
  .image-container {
  	display: flex;
  	justify-content: center;
  	align-items: center;
  }
  </style>