download-file.uvue 5.8 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>
13
          <button type="primary" @tap="downloadImagenal">下载</button>
14
        </view>
15
        <view style="margin: 10px;">
16 17
          <text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text>
          <button type="primary" @tap="downloadErrorFilename">下载</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
18 19 20
        </view>
      </view>
    </view>
21
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
22 23
  </scroll-view>
  <!-- #endif -->
24
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
25 26 27 28 29 30 31
<script>
  // #ifdef APP
  import {
    testInovkeDownloadFile,
    CommonOptions
  } from '@/uni_modules/test-invoke-network-api'
  // #endif
32

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

      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
107
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
108 109 110
          },
          fail: () => {
            this.jest_result = false
111
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
          }
        });
      },

      jest_set_cookie() {
        uni.request({
          url: "https://request.dcloud.net.cn/api/http/header/setCookie",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: true,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_download(true)
126
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
127 128 129
          },
          fail: () => {
            this.jest_result = false;
130
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144
          },
        });
      },

      jest_delete_cookie() {
        uni.request({
          url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: true,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_download(false)
145
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
146 147 148
          },
          fail: () => {
            this.jest_result = false;
149
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
          },
        });
      },
      jest_cookie_download(needCookie : boolean) {
        uni.downloadFile({
          url: "https://request.dcloud.net.cn/api/http/header/download",
          success: () => {
            this.jest_result = needCookie ? true : false;
          },
          fail: () => {
            this.jest_result = needCookie ? false : true;
          }
        });
      },
      jest_uts_module_invoked() {
        // #ifdef APP
        testInovkeDownloadFile({
          success: (res : any) => {
            this.jest_result = true
169
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
170 171
          },
          fail: (err : any) => {
172 173
            this.jest_result = false
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
174 175 176 177 178 179 180
          }
        } as CommonOptions)
        // #endif
      },
      jest_special_characters_download() {
        uni.downloadFile({
          url: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
181 182 183
          success: (res : DownloadFileSuccess) => {
            this.jest_result = true;
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
184 185
          },
          fail: () => {
186 187
            this.jest_result = false;
            this.jest_callback_triggred = true
DCloud-WZF's avatar
DCloud-WZF 已提交
188 189
          }
        });
190
      }
191
    }
192
  }
193 194 195
</script>

<style>
196 197 198
  .img {
    margin: 0 auto;
  }
W
wanganxp 已提交
199
</style>