uniapp使用uni.uploadFile上传图片文件,发送form-data请求

    • uni.uploadFile上传图片文件
    • uni.request发送form-data请求无效
    • uniapp不支持 new FormData
    • TypeError: Cannot read property ‘indexOf’ of undefined
    • 解决no multipart boundary was found

uni.uploadFile上传图片文件

使用uniapp开发混合app时碰到后台需要发送multipart/form-data请求 ,可以直接使用uni.uploadFile上传文件的同时将后台需要的其他 请求参数放在formData中一并发送

  1. 单文件发送请求
uni.uploadFile({ 
					url:this.url,
					filePath: tempFilePaths[0],
           			name: 'file',
					formData:this.formdata,
					header:{ 
						"Content-Type": "multipart/form-data",
						"token":this.token
					},
					success: (res) => { 
					 if (res.data.code == 200){ 
					 console.log('请求成功_______________',res)
						uni.showToast({ 
							icon:'none',
							title:'提交成功',
							success: (res) => { 
								setTimeout(() => { 
									uni.navigateBack({ 
										delta: 1
									})
								}, 1500)
							}
						})
       			 }
						
					},
					fail:(err)=>{ 
						console.log('请求失败_______________',err)
					}
				})
  1. 多文件发送
<--定义一个 file 对象的数组为files 参数,file 对象的结构:-->
let imgs = this.imgList.map((value, index) => { 
				    return { 
				            name: "img" + index, 
				            uri: value
				        }
				});

uni.uploadFile({ 
					url:this.url,
					files:this.imgs,
					formData:this.formdata,
					header:{ 
						"Content-Type": "multipart/form-data",
						"token":this.token
					},
					success: (res) => { 
					 if (res.data.code == 200){ 
					 console.log('请求成功_______________',res)
						uni.showToast({ 
							icon:'none',
							title:'提交成功',
							success: (res) => { 
								setTimeout(() => { 
									uni.navigateBack({ 
										delta: 1
									})
								}, 1500)
							}
						})
       			 }
						
					},
					fail:(err)=>{ 
						console.log('请求失败_______________',err)
					}
				})

uni.request发送form-data请求无效

uniapp不支持 new FormData

TypeError: Cannot read property ‘indexOf’ of undefined

解决no multipart boundary was found

本文地址:https://blog.csdn.net/qq_44090577/article/details/114257037