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

<style>
78 79 80
  .img {
    margin: 0 auto;
  }
W
wanganxp 已提交
81
</style>