uniappは、uni.uploadFileを使用して画像ファイルをアップロードし、フォームデータリクエストを送信します
uni.uploadFileアップロード画像ファイル
uniappを使用してハイブリッドアプリを開発する場合、バックグラウンドでmultipart / form-dataリクエストを送信する必要があります。uni.uploadFileを直接使用してファイルをアップロードし、バックグラウンドで必要な他のリクエストパラメータをformDataに入れて一緒に送信できます。
- 単一ファイル送信リクエスト
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)
}
})
- 複数のファイルの送信
<--定义一个 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は、フォームデータ要求の送信には無効です
ヘッダーのContent-Typeを「application / x-www-form-urlencoded」または「multipart / form-data」に変更すると、リクエストを正常に送信できません
uniappは新しいFormDataをサポートしていません
プロテストuniappは、直接の新しいFormData()および新しいwindow.FormData()をサポートしていません。