uniapp利用uni.uploadFile上传图片时在formData中携带token信息

上传图片时在formData中携带token信息

注意:在formData中携带参数时,不要配置header头信息,否则会造成后台接收不到前台传递的信息

// 上传头像
			chooseImg() {
				let that = this;
				let token = that.$tool.getCache('token')
				console.log('token',that.$tool.getCache('token'))
				uni.chooseImage({
					count: 1, //默认9
					// sizeType: ['original'], //, original 原图,compressed 压缩图,默认二者都有
					sourceType: ['album'], //从相册选择 'album' camera 
					success: function(res) {
						let tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							fileType: "image",
							url: that.$http.baseUrl + 'api/upload/image',
							filePath: tempFilePaths[0],
							name: 'iFile',							
							formData:{
								//iFile:tempFilePaths[0], // 这里一定不能加,加了 iOS 图片上传会失败
								token:token,
							},
							// 请求头一定要加,否则 iOS 图片上传会失败
						    header: {
						        'content-type': 'multipart/form-data' 
						    },							
							success: (res) => {
								console.log(res,"上传图片");
								if(res.statusCode == 200){
									if (JSON.parse(res.data).code == 500) {
										that.$tool.toast('请上传小于2M的图片')
										return
									}
									let avatar =   JSON.parse(res.data).data;
								}else{
									that.$tool.toast(res.message);
								}
							}
						});
					}
				});
			},

猜你喜欢

转载自blog.csdn.net/m0_52459016/article/details/122099149