我正在尝试使用Alamofire上传图像,但是似乎没有上传。
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpg")
multipartFormData.append(operaID.data, withName: "id")
},
to: URL_CORDINATE)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print("Upload image response:", response.result.value)
}
case .failure(let encodingError):
print("Error while uploading image:", encodingError)
}
我希望看到图片上传,但是主体似乎出了点问题(我应该将主体中的img和id传递为参数,而不是参数!)
但是它在邮递员中有效:https://i.stack.imgur.com/f0mUd.png
答案 0 :(得分:1)
您可以尝试以下操作:
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imgData!, withName: "img", fileName: "jpg", mimeType: "image/jpeg")
},
to: UPLOAD_IMAGE_URL,
encodingCompletion: { (encodingResult) -> Void in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
let result = response.data
do {
print("success")
}catch {
print("error")
}
}
case .failure(let encodingError):
print(encodingError)
}
})
答案 1 :(得分:0)
乍一看,它缺少HTTP方法:.post
,而mimeType应该是"image/jpeg"
,而不是"image/jpg"
。
看到这个:
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "data", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
multipartFormData.append(operaID.data, withName: "id")
},
to: YOUR_URL_HERE,
method: .post,
encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print("Upload image response:", response.result.value)
}
case .failure(let encodingError):
print("Error while uploading image:", encodingError)
}
})
是否有任何服务器日志指示错误?您可以在服务器代码中添加断点并在localhost上运行它以检查控制器中的内容吗?
如果服务器端点是http,请将其放入您的Info.plist
:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
答案 2 :(得分:0)
Alamofire.upload(multipartFormData: { multipartFormData in
if let img = image, let imageData = img.jpegData(compressionQuality: 0.6) {
multipartFormData.append(imageData, withName: imageName, fileName: "temp.jpg", mimeType: "image/jpg")
}
for (key, value) in parameter ?? [:] {
if value is String || value is Int {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
}
}, to: url, headers: headers, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _ , _ ):
upload.responseJSON { response in
debugPrint(response)
if response.response?.statusCode == 200
{
if let jsonData = try? JSONSerialization.data(withJSONObject: response.result.value!, options: .prettyPrinted) {
print("Response: \n",String(data: jsonData, encoding: String.Encoding.utf8) ?? "nil")
}
success(response.result.value! as AnyObject)
}else
{
let res = response.result.value! as AnyObject
let msg = res["Message"] as? String
if msg != nil
{
failure(msg!)
}else
{
failure("")
}
}
}
case .failure(let encodingError):
print(encodingError)
failure(encodingError.localizedDescription)
}
})