download-file.uvue 4.5 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
</template>
21
<script>
22 23

  // #ifdef APP
24 25 26 27
  import {
    testInovkeDownloadFile,
    CommonOptions
  } from '@/uni_modules/test-invoke-network-api'
28
  // #endif
29

30 31 32 33 34 35 36 37 38 39 40 41 42
  export default {
    data() {
      return {
        title: 'downloadFile',
        imageSrc: '',
        task: null as DownloadTask | null,
        //自动化测试例专用
        jest_result: false
      }
    },
    onLoad() {
    },
    onUnload() {
W
wanganxp 已提交
43
      // this.imageSrc = '';
44 45 46 47 48 49 50 51 52 53
      uni.hideLoading();
      this.task?.abort();
    },
    methods: {
      downloadImage: function () {
        uni.showLoading({
          title: '下载中'
        })
        var self = this
        this.task = uni.downloadFile({
雪洛's avatar
雪洛 已提交
54
          url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
55
          success: (res) => {
W
wanganxp 已提交
56
            console.log('downloadFile success, res is', res.tempFilePath)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
57
            self.imageSrc = res.tempFilePath;
58 59
          },
          fail: (err) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
60
            console.log('downloadFile fail, err is:', err)
61 62 63 64
          },
          complete: (res) => {
            uni.hideLoading();
            this.task = null;
65 66 67
          }
        });
        this.task?.onProgressUpdate((update) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
68
          console.log("progress : ", update.progress);
69 70 71 72 73
        })
      },
      //自动化测试例专用
      jest_downloadFile() {
        uni.downloadFile({
雪洛's avatar
雪洛 已提交
74
          url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
75 76 77 78 79 80 81
          success: () => {
            this.jest_result = true
          },
          fail: () => {
            this.jest_result = false
          }
        });
82 83
      },

84 85 86 87 88 89 90 91 92 93 94 95 96
      jest_downloadFile_with_uni_env() {
        uni.downloadFile({
          url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
          filePath: `${uni.env.CACHE_PATH}/a/b/`,
          success: () => {
            this.jest_result = true
          },
          fail: () => {
            this.jest_result = false
          }
        });
      },

97 98
      jest_set_cookie(){
        uni.request({
雪洛's avatar
雪洛 已提交
99
          url: "https://request.dcloud.net.cn/api/http/header/setCookie",
100 101 102
          method: "GET",
          timeout: 6000,
          sslVerify: false,
雪洛's avatar
雪洛 已提交
103
          withCredentials: true,
104 105 106 107 108 109 110 111 112 113 114 115
          firstIpv4: false,
          success: () => {
            this.jest_cookie_download(true)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },

      jest_delete_cookie(){
        uni.request({
雪洛's avatar
雪洛 已提交
116
          url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
117 118 119
          method: "GET",
          timeout: 6000,
          sslVerify: false,
雪洛's avatar
雪洛 已提交
120
          withCredentials: true,
121 122 123 124 125 126 127 128 129 130 131
          firstIpv4: false,
          success: () => {
            this.jest_cookie_download(false)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_cookie_download(needCookie: boolean){
        uni.downloadFile({
雪洛's avatar
雪洛 已提交
132
          url: "https://request.dcloud.net.cn/api/http/header/download",
133 134 135 136 137 138 139
          success: () => {
            this.jest_result = needCookie ? true : false;
          },
          fail: () => {
            this.jest_result = needCookie ? false : true;
          }
        });
140 141
      },
      jest_uts_module_invoked(){
142
        // #ifdef APP
143 144 145 146 147 148 149 150
        testInovkeDownloadFile({
          success:(res: any)=>{
            this.jest_result = true
          },
          fail:(err: any)=>{
            this.jest_result = false
          }
        } as CommonOptions)
151
        // #endif
152 153 154 155 156 157 158 159 160 161 162
      },
      jest_special_characters_download(){
        uni.downloadFile({
          url: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
          success: (res: DownloadFileSuccess) => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          }
        });
163
      }
164
    }
165
  }
166 167 168
</script>

<style>
169 170 171
  .img {
    margin: 0 auto;
  }
W
wanganxp 已提交
172
</style>