如何在 swift 5 中使用 Alamofire multipartFormData 发送数据和图像
How to send data along with an image using Alamofire multipartFormData in swift 5
我正在尝试使用 Alamofire multipartFormData 更新配置文件的详细信息。下面是请求的 Postman 截图。
下面是我在 Alamofire 中使用 multipartFormData 尝试做的事情。
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(data: Data, withName: self.Pharmacy_name.text!)
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
为了 API 调用 return “true”,上述所有数据都应该在请求中。我只是不知道如何将文本字段值附加到 multipartFormData.append.
我是 iOS 开发的新手,因此请多多包涵。非常感谢我能得到的任何帮助!
修好了!下面是代码。
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-Type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(self.Pharmacy_name.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "name")
multipartFormData.append(self.Address_line1.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_1")
multipartFormData.append(self.Address_line2.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_2")
multipartFormData.append(self.Start_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "open_at")
multipartFormData.append(self.close_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "close_at")
multipartFormData.append(self.contact_number.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "phone_no")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "longitude")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "latitude")
multipartFormData.append("1".data(using: .utf8, allowLossyConversion: false)!, withName: "id")
multipartFormData.append(self.token.data(using: .utf8, allowLossyConversion: false)!, withName: "api_token")
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
希望这对某人有所帮助。
我正在尝试使用 Alamofire multipartFormData 更新配置文件的详细信息。下面是请求的 Postman 截图。
下面是我在 Alamofire 中使用 multipartFormData 尝试做的事情。
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(data: Data, withName: self.Pharmacy_name.text!)
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
为了 API 调用 return “true”,上述所有数据都应该在请求中。我只是不知道如何将文本字段值附加到 multipartFormData.append.
我是 iOS 开发的新手,因此请多多包涵。非常感谢我能得到的任何帮助!
修好了!下面是代码。
func Post(imageOrVideo : UIImage?){
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-Type": "multipart/form-data"
]
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "landing.jpeg", mimeType: "image/jpeg")
multipartFormData.append(self.Pharmacy_name.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "name")
multipartFormData.append(self.Address_line1.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_1")
multipartFormData.append(self.Address_line2.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "address_line_2")
multipartFormData.append(self.Start_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "open_at")
multipartFormData.append(self.close_time.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "close_at")
multipartFormData.append(self.contact_number.text!.data(using: .utf8, allowLossyConversion: false)!, withName: "phone_no")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "longitude")
multipartFormData.append("0.00".data(using: .utf8, allowLossyConversion: false)!, withName: "latitude")
multipartFormData.append("1".data(using: .utf8, allowLossyConversion: false)!, withName: "id")
multipartFormData.append(self.token.data(using: .utf8, allowLossyConversion: false)!, withName: "api_token")
},
to: "url", method: .post , headers: headers)
.response { resp in
print("response is:" , resp)
}
希望这对某人有所帮助。