我正在尝试在发布请求中发送图像,但它并没有发送到服务器端,我正在使用多部分程序进行此操作,我不知道为什么会这样,甚至响应都没有正确。这是我的代码,
func addAPI()
{
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-type": "multipart/form-data"
]
let parameters :[String: Any] = ["name":productNameTxt.text!,
"price":priceTxt.text!,
"size": sizeTxt.text!,
"weight":weightTxt.text!,
"quality":qualityTxt.text!,
"brand":brandTxt.text!,
"shippingCost":shippingCostTxt.text!,
"details":detailTxt.text!,
"material_id":"\(materialId)",
"material_type_id": "\(subMaterialId)",
"maxQuantity":"4"]
print(parameters)
Alamofire.upload(multipartFormData: { multipartFormData in
for (key, value) in parameters {
if let data = ((value) as AnyObject).data(using: String.Encoding.utf8.rawValue) {
multipartFormData.append(data, withName: key)
}
}
// for (index, image) in self.imageArray.enumerated() {
// multipartFormData.append(image.pngData()!, withName: "file", fileName: "image\(index).png", mimeType: "image/png")
// }
for i in 0..<self.imageArray.count{
let imageData1 = self.imageArray[i].jpegData(compressionQuality: 0.6)
multipartFormData.append(imageData1!, withName: "morephoto[\(i)]" , fileName: "photo" + String(i) + ".jpg", mimeType: "image/jpeg")
}
},
to: addProductUrl,method:HTTPMethod.post,
headers:headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload
.validate()
.responseJSON { response in
switch response.result {
case .success(let value):
// compBlock(value as AnyObject,true)
print("responseObject: \(value)")
print(response.data,response.value,response.error)
case .failure(let responseError):
print("responseError: \(responseError)")
print(response.data,response.value,response.error)
}
}
case .failure(let encodingError):
print("encodingError: \(encodingError)")
let errorDesc = (encodingError as NSError).localizedDescription
//failure(errorDesc as NSString,false)
}
})
}
我犯了什么错误?