[英]How to upload image to aws pre signed url as multipart data using Alamofire 5?
I am trying to upload an image to the pre-signed AWS URL.我正在尝试将图像上传到预先签名的 AWS URL。 I tried with Alamofire 5 upload method which accepts multipartData.
我尝试使用接受 multipartData 的 Alamofire 5 上传方法。 I am getting a 403 error with Alamofire 5 multipart upload.
我在使用 Alamofire 5 分段上传时遇到 403 错误。
When I try with URLSession it is working properly.当我尝试使用 URLSession 时,它工作正常。
/// Working code
var request: URLRequest = URLRequest(url: requestURL)
request.httpMethod = "PUT"
request.httpBody = image
request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
let tasksession: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (response, urlResp, error) in
if let data: Data = response {
do {
let json: [String: Any]? = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
debugPrint("json \(json)")
}
catch {
debugPrint("error \(error)")
}
}
print(response ?? "response nil")
print(error ?? "response nil")
})
tasksession.resume()
Same when I try with AF 5 it is not working当我尝试使用 AF 5 时也一样,它不起作用
// Not working code. // 不工作的代码。
AF.upload(multipartFormData: { (multiPart) in
multiPart.append(imageData, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
}, to: url, method: .put, headers: ["Content-Type": "image/jpeg"],
])
.uploadProgress(queue: .main, closure: { progress in
//Current upload progress of the file
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON(completionHandler: { data in
let json: [String: Any]? = data as? [String: Any]
debugPrint("upload complete json \(data)")
})
Could someone tell why it is not working with multipart upload with Alamofire 5, multipart upload is working in Android.有人可以告诉为什么它不能使用 Alamofire 5 进行分段上传,分段上传在 Android 中有效。
Alamofire Version 4.9.1 Alamofire 版本 4.9.1
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
//Any Post Params if you have.
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:uploadUrlStr) //uploadUrlStr: upload url in your case
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Uploading")
print(CGFloat(progress.fractionCompleted * 100))
})
upload.responseJSON { response in
print("Upload Finished")
guard let resultValue = response.result.value else {
NSLog("Result value in response is nil")
return
}
}
case .failure(let encodingError):
print(encodingError.localizedDescription)
}
}
This is working for me.这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.