Swift 3 Alamofire 4 上传带参数的图片数组
Swift 3 Alamofire 4 Upload array of images with parameters
我正在尝试使用 Alamofire 4 和 Swift 3 上传一系列图片以及一些参数。
参数似乎有效,因为更新已完成,但图像未到达服务器
在 Postman 中我可以毫无问题地做到这一点:
Postman request sample
到目前为止,这是我的代码:
let parameters = [
"service_request_id" : servicesID,
"status_id" : "4",
]
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
},
to: ConnectionWS.UpdateServicesURL,
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
// If the request to get activities is succesfull, store them
if response.result.isSuccess{
print(response.debugDescription)
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:
{
self.delegate?.statusChanged(IsFinish: false)
})
})
// Else throw an error
} else {
var errorMessage = "ERROR MESSAGE: "
if let data = response.data {
// Print message
let responseJSON = JSON(data: data)
if let message: String = responseJSON["error"]["message"].string {
if !message.isEmpty {
errorMessage += message
}
}
}
print(errorMessage) //Contains General error message or specific.
print(response.debugDescription)
}
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:nil)
})
}
case .failure(let encodingError):
print("FALLE ------------")
print(encodingError)
}
}
)
我是不是做错了什么?
请帮忙。
所以错误来自服务器端,它对图像的大小限制非常小,这就是它们没有保存的原因。我更新了来自
的 JPEG 图像的压缩
if let imageData = UIImageJPEGRepresentation(image, 1)
至
if let imageData = UIImageJPEGRepresentation(image, 0.6)
现在一切正常。
感谢@Sneak 提供的链接。
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
//change this code to below
for (key,value) in parameters {
if let value = value as? String {
if value == "image" {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
} else {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
}
我正在尝试使用 Alamofire 4 和 Swift 3 上传一系列图片以及一些参数。
参数似乎有效,因为更新已完成,但图像未到达服务器
在 Postman 中我可以毫无问题地做到这一点:
Postman request sample
到目前为止,这是我的代码:
let parameters = [
"service_request_id" : servicesID,
"status_id" : "4",
]
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
},
to: ConnectionWS.UpdateServicesURL,
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print(progress)
})
upload.responseJSON { response in
// If the request to get activities is succesfull, store them
if response.result.isSuccess{
print(response.debugDescription)
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:
{
self.delegate?.statusChanged(IsFinish: false)
})
})
// Else throw an error
} else {
var errorMessage = "ERROR MESSAGE: "
if let data = response.data {
// Print message
let responseJSON = JSON(data: data)
if let message: String = responseJSON["error"]["message"].string {
if !message.isEmpty {
errorMessage += message
}
}
}
print(errorMessage) //Contains General error message or specific.
print(response.debugDescription)
}
alert.dismiss(animated: true, completion:
{
self.dismiss(animated: true, completion:nil)
})
}
case .failure(let encodingError):
print("FALLE ------------")
print(encodingError)
}
}
)
我是不是做错了什么?
请帮忙。
所以错误来自服务器端,它对图像的大小限制非常小,这就是它们没有保存的原因。我更新了来自
的 JPEG 图像的压缩if let imageData = UIImageJPEGRepresentation(image, 1)
至
if let imageData = UIImageJPEGRepresentation(image, 0.6)
现在一切正常。
感谢@Sneak 提供的链接。
for (key,value) in parameters {
if let value = value as? String {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
for (image) in self.imagesArray {
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
}
}
//change this code to below
for (key,value) in parameters {
if let value = value as? String {
if value == "image" {
multipartFormData.append(imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
} else {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}
}