uniapp 上传图片及base64图片上传

 普通的上传图片



change: function() {
	var url = this.Url;
	uni.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'],
		sourceType: ['album'],
	    success: (chooseImageRes) => {
		    const tempFilePaths = chooseImageRes.tempFilePaths;
			var that=this;
		    uni.uploadFile({
				 url: url + '上传图片的接口',
				 filePath: tempFilePaths[0],
				 name: 'file',
				 formData: {
				     要传的数据
				 },
				 success: (uploadFileRes) => {
				      uni.hideLoading()
				      var infoMessage = JSON.parse(uploadFileRes.data);
				      if (infoMessage.code == 1) {
					        that.imgUrl = infoMessage.data;      // 图片地址
						    this.weburl=this.websiteUrl;         // 域名
						    this.hear=this.weburl+that.imgUrl
				             uni.showToast({
				                title: '上传成功',
				               	icon: 'none',
				             })
				       } else {
				           uni.showToast({
				              title: infoMessage.message,
				              icon: 'none',
				            })
				       }
				}
		})
        }
	})
},

base64     上传多张图片(数组)


	
import { pathToBase64 , base64ToPath } from 'image-tools'
data() {
	return {
		imgArr: [],
		imagearr: []
    }
},
// 上传图片
upLoadImg: function() {
	var websiteUrl = this.websiteUrl;         //调域名
	var token = uni.getStorageSync('token');  //传 token
	var that = this;
		if (that.imgArr.length > 0) {
			that.imgArr = [];
		}
		console.log(typeof(that.imgArr));
		uni.chooseImage({
			count: 9,
			sizeType: ['original', 'compressed'],
			sourceType: ['album'],
			success: (res) => {
				that.imagearr = res.tempFilePaths;
				for(let i = 0; i < res.tempFilePaths.length; i++){
					pathToBase64(res.tempFilePaths[i])
				.then(base64 => {
					// console.log(base64)
				    that.imgArr.push(base64);
		})
	}				
	}
	});
	},
//其他要上传这些图片的接口
submitInfo: function(types) {
	//console.log(this.imgArr);
	//console.log(typeof(this.imgArr));
	uni.showLoading({
		title: '请稍后',
		mask: true
	})
	uni.request({
		url: this.websiteUrl + '接口',
		data: {
			image: this.imgArr,  //上传上面的那些图片
		    token: uni.getStorageSync('token')
		},
		method: 'POST',
		header: {
			 'content-type': 'application/json',
		},
		success: (res) => {
			console.log(res.data);
			uni.hideLoading();
		    uni.showToast({
				title: res.data.message,
				icon: 'none'
		    })
			if (res.data.code == 1) {
				this.otherAdvice='';
				this.imagearr='';
				this.imgArr=''
		    }
		},fail:(res)=>{
			uni.hideLoading();
		}
	});
}
			

你可能感兴趣的:(uniapp,uni-app)