发送带有特定参数的图像?
Send image with certain parameters?
我发送纯文本没问题。
比如我有这个要求:
func buyWholeSaler(url : String, token:String, manager:String, fio:String,phone:String, sender:String, carrier:String, document:String,shipping_id:String,pay_type:String, shipping_pay:String, amount_nalogi:String, shipping_return:String,money_recipient :String,comment:String, completion: @escaping (JSON)-> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.request(url,method: .post, parameters: parameters, headers:headers).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
completion(json)
case .failure(let error):
print(error)
}
}
}
但是如何发送带有这些参数的图片?
我有 2 张图片
var image1 = UIImage()
var image2 = UIImage()
如何重写请求才能正确发送?例如:
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
...
"ur_file_pdv": image1,
"ur_file_er": image1
]
我找到了这个请求,但我不明白它是如何工作的:
AF.upload(multipartFormData: { multiPart in
for p in parameters {
multiPart.append("\(p.value)".data(using: String.Encoding.utf8)!, withName: p.key)
}
multiPart.append(imageVityagPdv.jpegData(compressionQuality: 0.4)!, withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multiPart.append(imageVityag.jpegData(compressionQuality: 0.4)!, withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
}, to: url, method: .post, headers: headers) .uploadProgress(queue: .main, closure: { progress in
print("Upload Progress: \(progress.fractionCompleted)")
}).responseJSON(completionHandler: { data in
print("upload finished: \(data)")
let success = json["success"].stringValue
}).response { (response) in
switch response.result {
case .success(let resut):
print("upload success result: \(resut!)")
case .failure(let err):
print("upload err: \(err)")
}
}
好像是在上传东西到服务器,但是没有下单
您不能将 "Data" 添加为参数,特别是因为 "request" 用于从服务器检索数据并且应该非常小,只需向服务器提供您实际使用的信息想从中接收(以文本形式)。要上传内容,您可以使用 POST 请求,您可以在 Alamofire 中使用 AF.upload 执行此操作,如下所示。
"order does not happen" 是什么意思?也许你有一些服务器代码,所以我们可以看看问题是什么?可能是它想要参数作为 GET,在这种情况下,您可以将它们直接添加到 URL.
func buyWholeSaler(url : String, token: String, manager: String,
fio: String, phone: String, sender: String, carrier: String,
document: String, shipping_id: String, pay_type: String,
shipping_pay: String, amount_nalogi: String,
shipping_return: String, money_recipient: String, comment:String,
completion: @escaping (JSON) -> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.upload(multipartFormData: { (multipartFormData) in
parameters.forEach({ multipartFormData.append("\(p.value)".data(using: .utf8)!, withName: p.key) })
multipartFormData.append(image1.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multipartFormData.append(image2.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
},
to: url, method: .post, headers : headers).responseJSON { (response) in
switch response.result {
case .success(let value): completion(JSON(value))
case .failure(let error): print(error)
}
}
}
self.uploadMultipart(buildedWith: { (multipartData) in
multipartData.append(assestPath, withName: "file", fileName: "mp4", mimeType: "")
assest路径为图片url,应该是本地路径,也可以转成数据形式,不过直接append path会更好。
我发送纯文本没问题。
比如我有这个要求:
func buyWholeSaler(url : String, token:String, manager:String, fio:String,phone:String, sender:String, carrier:String, document:String,shipping_id:String,pay_type:String, shipping_pay:String, amount_nalogi:String, shipping_return:String,money_recipient :String,comment:String, completion: @escaping (JSON)-> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.request(url,method: .post, parameters: parameters, headers:headers).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
completion(json)
case .failure(let error):
print(error)
}
}
}
但是如何发送带有这些参数的图片?
我有 2 张图片
var image1 = UIImage()
var image2 = UIImage()
如何重写请求才能正确发送?例如:
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
...
"ur_file_pdv": image1,
"ur_file_er": image1
]
我找到了这个请求,但我不明白它是如何工作的:
AF.upload(multipartFormData: { multiPart in
for p in parameters {
multiPart.append("\(p.value)".data(using: String.Encoding.utf8)!, withName: p.key)
}
multiPart.append(imageVityagPdv.jpegData(compressionQuality: 0.4)!, withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multiPart.append(imageVityag.jpegData(compressionQuality: 0.4)!, withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
}, to: url, method: .post, headers: headers) .uploadProgress(queue: .main, closure: { progress in
print("Upload Progress: \(progress.fractionCompleted)")
}).responseJSON(completionHandler: { data in
print("upload finished: \(data)")
let success = json["success"].stringValue
}).response { (response) in
switch response.result {
case .success(let resut):
print("upload success result: \(resut!)")
case .failure(let err):
print("upload err: \(err)")
}
}
好像是在上传东西到服务器,但是没有下单
您不能将 "Data" 添加为参数,特别是因为 "request" 用于从服务器检索数据并且应该非常小,只需向服务器提供您实际使用的信息想从中接收(以文本形式)。要上传内容,您可以使用 POST 请求,您可以在 Alamofire 中使用 AF.upload 执行此操作,如下所示。
"order does not happen" 是什么意思?也许你有一些服务器代码,所以我们可以看看问题是什么?可能是它想要参数作为 GET,在这种情况下,您可以将它们直接添加到 URL.
func buyWholeSaler(url : String, token: String, manager: String,
fio: String, phone: String, sender: String, carrier: String,
document: String, shipping_id: String, pay_type: String,
shipping_pay: String, amount_nalogi: String,
shipping_return: String, money_recipient: String, comment:String,
completion: @escaping (JSON) -> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.upload(multipartFormData: { (multipartFormData) in
parameters.forEach({ multipartFormData.append("\(p.value)".data(using: .utf8)!, withName: p.key) })
multipartFormData.append(image1.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multipartFormData.append(image2.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
},
to: url, method: .post, headers : headers).responseJSON { (response) in
switch response.result {
case .success(let value): completion(JSON(value))
case .failure(let error): print(error)
}
}
}
self.uploadMultipart(buildedWith: { (multipartData) in
multipartData.append(assestPath, withName: "file", fileName: "mp4", mimeType: "")
assest路径为图片url,应该是本地路径,也可以转成数据形式,不过直接append path会更好。