123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <template>
- <!-- #ifdef APP -->
- <scroll-view class="page-scroll-view">
- <!-- #endif -->
- <view>
- <page-head :title="title"></page-head>
- <view class="uni-padding-wrap uni-common-mt">
- <view class="demo">
- <image v-if="imageSrc" :src="imageSrc" class="image" mode="widthFix"></image>
- <text v-else class="uni-hello-addfile" @click="chooseImage">+ 选择图片</text>
- </view>
- </view>
- </view>
- <!-- #ifdef APP -->
- </scroll-view>
- <!-- #endif -->
- </template>
- <script>
- // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
- import {
- testInovkeUploadFile,
- CommonOptions
- } from '@/uni_modules/test-invoke-network-api'
- // #endif
- 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
- });
- },
- complete: (res) => {
- uni.hideLoading();
- this.task = null
- }
- });
- },
- fail: (err) => {
- console.log('chooseImage fail', err)
- }
- })
- },
- //自动化测试例专用
- jest_uploadFile() {
- const imageSrc = "/static/uni.png";
- uni.uploadFile({
- url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
- filePath: imageSrc,
- name: 'file',
- formData: {
- 'user': 'test'
- },
- success: () => {
- this.jest_result = true;
- },
- fail: () => {
- this.jest_result = false;
- },
- })
- },
- jest_uploadFile_with_uni_env() {
- /**
- * 微信小程序只支持USER_DATA_PATH,且子目录未创建的情况下不能直接下载到子目录内
- */
- const filePath = `${uni.env.USER_DATA_PATH}/uni-app.png`
- uni.downloadFile({
- url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
- filePath: filePath,
- success: () => {
- uni.uploadFile({
- url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
- filePath: filePath,
- name: 'file',
- success: () => {
- this.jest_result = true;
- },
- fail: () => {
- this.jest_result = false;
- },
- })
- },
- fail: () => {
- this.jest_result = false
- }
- });
- },
- jest_set_cookie() {
- uni.request({
- url: "https://request.dcloud.net.cn/api/http/header/setCookie",
- 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({
- url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
- 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";
- uni.uploadFile({
- url: 'https://request.dcloud.net.cn/api/http/header/upload',
- 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";
- uni.uploadFile({
- 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;
- },
- })
- },
- jest_uts_module_invoked() {
- // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
- testInovkeUploadFile({
- success: (res : any) => {
- this.jest_result = true
- },
- fail: (err : any) => {
- this.jest_result = false
- }
- } as CommonOptions)
- // #endif
- }
- }
- }
- </script>
- <style>
- .image {
- width: 100%;
- }
- .demo {
- background: #fff;
- padding: 25px;
- justify-content: center;
- align-items: center;
- }
- .uni-hello-addfile {
- text-align: center;
- background: #fff;
- padding: 25px;
- margin-top: 10px;
- font-size: 19px;
- color: #808080;
- }
- </style>
|