微信小程序-file文件流上传图片

小程序的图片上传功能,官网已给出了比较详细的API(image)

这里说下将上传后的图片提交给服务器,但是微信的上传图片功能不支持批量上传,所以目前只能通过for循环进行上传到服务器

uni.showLoading({
	title: '正在上传',
})
this.imgPaths.forEach((item,index) => {
	uni.uploadFile({
		url:api.busines.uploadImg, // 后台服务URL
		filePath: item, // 微信返回的临时图片地址
		name: 'files',
		header:{
			"Content-Type": "multipart/form-data"
		},
		formData:{// 其他参数
			siteId: that.siteId
		},
		success: (res) => {
			let result = JSON.parse(res.data)
			if (index === (this.imgPaths.length - 1)) {// 判断是否为最后一个图片
			if (result.code === enums.code.REQUEST_SUCCESS) {
				uni.showModal({
					title:"温馨提示",
					content:"上传成功",
					showCancel: false,
					success:function(res){
						let pages = getCurrentPages()
						// #ifdef MP-WEIXIN  
						let previousPage = pages[pages.length - 2].$vm //上一个页面
						// #endif
						// #ifdef H5
						let previousPage = pages[pages.length - 2] //上一个页面
					        // #endif
						previousPage.setData({
							isShowImg: false
						})
						uni.navigateBack({
						    delta: 1
						});
					}
				})
			} else {// 错误提示
				uni.showModal({
					title:"温馨提示",
					content:result.message,
					showCancel: false,
					success:function(res){}
				})
			        uni.showToast({
					title: result.message,
					icon: "none",
					duration: 3000
				})
			}
			uni.hideLoading();
		}
	})
})                    

  这样就可以将上传的图片通过文件流传给后台服务器

猜你喜欢

转载自www.cnblogs.com/WQLong/p/12525610.html
今日推荐