uniappは、uni.uploadFileを使用して画像ファイルをアップロードし、フォームデータリクエストを送信します

uni.uploadFileアップロード画像ファイル

uniappを使用してハイブリッドアプリを開発する場合、バックグラウンドで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は、フォームデータ要求の送信には無効です

ヘッダーのContent-Typeを「application / x-www-form-urlencoded」または「multipart / form-data」に変更すると、リクエストを正常に送信できません

uniappは新しいFormDataをサポートしていません

プロテストuniappは、直接の新しいFormData()および新しいwindow.FormData()をサポートしていません。
ここに画像の説明を挿入

TypeError:未定義のプロパティ 'indexOf'を読み取れません

おすすめ

転載: blog.csdn.net/qq_44090577/article/details/114257037