upload-file.uvue 7.0 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
4
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="demo">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
9 10
          <image v-if="imageSrc" :src="imageSrc" class="image" mode="widthFix"></image>
          <text v-else class="uni-hello-addfile" @click="chooseImage">+ 选择图片</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
11
        </view>
雪洛's avatar
雪洛 已提交
12 13
      </view>
      <button @click="jest_uploadFile_with_uni_env">jest_uploadFile_with_uni_env</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
14
    </view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
15
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
16 17
  </scroll-view>
  <!-- #endif -->
18
</template>
19 20 21 22 23 24 25
<script>
  // #ifdef APP
  import {
    testInovkeUploadFile,
    CommonOptions
  } from '@/uni_modules/test-invoke-network-api'
  // #endif
26

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  export default {
    data() {
      return {
        title: 'uploadFile',
        imageSrc: '',
        task: null as UploadTask | null,
        //自动化测试例专用
        jest_result: false,
      }
    },
    onLoad() {
    },
    onUnload() {
      this.imageSrc = '';
      uni.hideLoading();
      this.task?.abort();
    },
    methods: {
      chooseImage: function () {
        uni.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: ['album'],
          success: (res) => {
            console.log('chooseImage success, temp path is', res.tempFilePaths[0])
            var imageSrc = res.tempFilePaths[0]
            uni.showLoading({
              title: '上传中'
            })
            this.task = uni.uploadFile({
              url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
              filePath: imageSrc,
              name: 'file',
              formData: {
                'user': 'test'
              },
              success: (res) => {
                console.log('uploadImage success, res is:', res)
                uni.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 1000
                })
                this.imageSrc = imageSrc
              },
              fail: (err) => {
                console.log('uploadImage fail', err);
                uni.showModal({
                  content: err.errMsg,
                  showCancel: false
                });
DCloud-WZF's avatar
DCloud-WZF 已提交
78 79 80 81
              },
              complete: (res) => {
                uni.hideLoading();
                this.task = null
82
              }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
83 84 85 86 87 88 89 90 91 92
            });
          },
          fail: (err) => {
            console.log('chooseImage fail', err)
          }
        })
      },
      //自动化测试例专用
      jest_uploadFile() {
        const imageSrc = "/static/uni.png";
93
        uni.uploadFile({
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
          url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
          filePath: imageSrc,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: () => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          },
        })
H
hdx 已提交
107
      },
雪洛's avatar
雪洛 已提交
108 109 110 111 112
      jest_uploadFile_with_uni_env() {
        /**
         * 微信小程序只支持USER_DATA_PATH,且子目录未创建的情况下不能直接下载到子目录内
         */
        const filePath = `${uni.env.USER_DATA_PATH}/uni-app.png`
113 114 115 116
        uni.downloadFile({
          url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
          filePath: filePath,
          success: () => {
DCloud-WZF's avatar
DCloud-WZF 已提交
117 118 119 120 121 122 123 124 125 126 127
            uni.uploadFile({
              url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
              filePath: filePath,
              name: 'file',
              success: () => {
                this.jest_result = true;
              },
              fail: () => {
                this.jest_result = false;
              },
            })
128 129 130 131 132 133
          },
          fail: () => {
            this.jest_result = false
          }
        });
      },
H
hdx 已提交
134 135
      jest_set_cookie() {
        uni.request({
雪洛's avatar
雪洛 已提交
136
          url: "https://request.dcloud.net.cn/api/http/header/setCookie",
H
hdx 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_upload(true)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },

      jest_delete_cookie() {
        uni.request({
雪洛's avatar
雪洛 已提交
153
          url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
H
hdx 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
          method: "GET",
          timeout: 6000,
          sslVerify: false,
          withCredentials: false,
          firstIpv4: false,
          success: () => {
            this.jest_cookie_upload(false)
          },
          fail: () => {
            this.jest_result = false;
          },
        });
      },
      jest_cookie_upload(needCookie : boolean) {
        const imageSrc = "/static/uni.png";
169
        uni.uploadFile({
雪洛's avatar
雪洛 已提交
170
          url: 'https://request.dcloud.net.cn/api/http/header/upload',
H
hdx 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
          filePath: imageSrc,
          name: 'file',
          success: (res : UploadFileSuccess) => {
            const data = JSON.parseObject(res.data)
            const errCode = data?.getNumber("errCode")
            if (errCode != null && errCode == 1000) {
              this.jest_result = needCookie ? false : true;
            } else {
              this.jest_result = needCookie ? true : false;
            }
          },
          fail: () => {
            this.jest_result = false;
          },
        })
      },
      jest_files_upload() {
        const imageSrc = "/static/uni.png";
189
        uni.uploadFile({
H
hdx 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
          url: 'https://unidemo.dcloud.net.cn/upload',
          files: [
            {
              name: "file1",
              uri: imageSrc
            } as UploadFileOptionFiles,
            {
              name: "file2",
              uri: imageSrc
            } as UploadFileOptionFiles
          ],
          success: (res : UploadFileSuccess) => {
            if (res.statusCode == 200) {
              this.jest_result = true;
            }
          },
          fail: () => {
            this.jest_result = false;
          },
        })
210 211 212 213 214 215 216 217 218 219 220 221
      },
      jest_uts_module_invoked() {
        // #ifdef APP
        testInovkeUploadFile({
          success: (res : any) => {
            this.jest_result = true
          },
          fail: (err : any) => {
            this.jest_result = false
          }
        } as CommonOptions)
        // #endif
H
hdx 已提交
222
      }
223
    }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
224
  }
225 226 227
</script>

<style>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
228 229 230
  .image {
    width: 100%;
  }
231

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
232 233
  .demo {
    background: #fff;
H
hdx 已提交
234
    padding: 25px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
235 236 237
    justify-content: center;
    align-items: center;
  }
238

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
239 240 241
  .uni-hello-addfile {
    text-align: center;
    background: #fff;
H
hdx 已提交
242
    padding: 25px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
243
    margin-top: 10px;
H
hdx 已提交
244
    font-size: 19px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
245 246
    color: #808080;
  }
247
</style>