我不确定如何使用Alamofire将图像上传到Uploadcare.com (reference)。我应该给图像或二进制数组的路径?我不确定将字符串转换为Data
对象是否正确
func uploadPhoto(imageToUpload : UIImage){
let keyJson = ("<my_api_key>").data(using: .utf8)!
let uploadStore = "1".data(using: .utf8)
let imageData = UIImageJPEGRepresentation(imageToUpload, 0.2)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(keyJson, withName: "UPLOADCARE_PUB_KEY")
multipartFormData.append(uploadStore!, withName: "UPLOADCARE_STORE")
multipartFormData.append(imageData!, withName: "file")
},
to: "https://upload.uploadcare.com/base/",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
debugPrint(encodingError)
}
}
)
}
结果:
[Response]: <NSHTTPURLResponse: 0x6100002390a0> { URL: https://upload.uploadcare.com/base/ } { status code: 400, headers {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Headers" = "X-PINGOTHER, DNT";
"Access-Control-Allow-Methods" = "POST, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Access-Control-Max-Age" = 1;
Connection = "keep-alive";
"Content-Length" = 31;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 09 Oct 2016 18:41:45 GMT";
Server = nginx;
"X-Frame-Options" = DENY;
} }
答案 0 :(得分:0)
我可以看到有两种方法可以用Alamofire做到这一点。您可以使用像public func append(_ fileURL: URL, withName name: String)
这样的文件URL的函数,并为它们提供文件的URL。或者,您可以将图像转换为数据,并使用public func append(_ data: Data, withName name: String)