小程序上传图片

// 点击打开文件选择要传输的图片
			chooseImage() {
				let that = this;
				uni.chooseImage({
					count: 9, // 默认9
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
					success(res) {
						let tempFilePaths = res.tempFilePaths; // 返回选定照片的本地文件路径列表,          tempFilePath可以作为img标签的src属性显示图片
						console.log(tempFilePaths)
						that.upload({
							path: tempFilePaths
						})
					}
				})
			},
			//上传图片
			upload(data) { // 上传图片
				var that = this,
					i = data.i ? data.i : 0,
					success = data.success ? data.success : 0,
					fail = data.fail ? data.fail : 0;
				uni.showToast({
						icon: "loading",
						title: "正在上传"
					}),
				uni.uploadFile({
					//上传图片协议接口
					url: 'https://dttll.dinoopnn.com/api/oss/upload',
					filePath: data.path[i],
					name: 'file',
					header: {
						"Content-Type": "multipart/form-data",
					    'Authorization': 'Bearer ' + uni.getStorageSync('token')
					},
					formData: {
						type: "driver"
					},
					success(res) {
						const code = res.statusCode.toString()
						if (code.startsWith('2') || code === '304') {
							success++;
							let arr = JSON.parse(res.data);
							let urlList = that.urlList.concat(arr.data);
							that.urlList=urlList;
						} else if (code == -1) {
							uni.showModal({
								title: '提示',
								content: '登录状态过期,是否重新登录',
								showCancel: false,
								confirmText: '是',
								success(res) {
									if (res.confirm) {
										console.log('用户点击确定')
									}
								}
							})
						} else {
							fail++;
							uni.showModal({
								title: '提示',
								content: '上传失败',
								showCancel: false
							})
							return;
						}
					},
					fail(e) {
						fail++;
						uni.showModal({
							title: '提示',
							content: '上传失败',
							showCancel: false
						})
					},
					complete() {
						uni.hideToast(); //隐藏Toast
						console.log(i);
						i++;
						if (i == data.path.length) { //当图片传完时,停止调用   
							//this.save();//上传完后再执行的方法
							console.log('执行完毕');
							console.log('成功:' + success + " 失败:" + fail);
						} else { //若图片还没有传完,则继续调用函数
							console.log(i);
							data.i = i;
							data.success = success;
							data.fail = fail;
							that.upload(data);
						}
					}

				})
			},
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值