使用 Alamofire 将图像上传到 API 服务器
Posted
技术标签:
【中文标题】使用 Alamofire 将图像上传到 API 服务器【英文标题】:Uploading Image to API Server using Alamofire 【发布时间】:2017-02-12 14:29:31 【问题描述】:我有一个关于的问题。
基本网址:http://vietnamtravelguide-app.com/quangnam/api_inquangnam/tips/create?user_id=""&token="" 3个参数:location_id、content、imageName
邮递员发帖没问题
我的代码:
Alamofire.upload(multipartFormData: multipartFormData in
multipartFormData.append(image, withName: "imageName", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
, with: path, encodingCompletion: encodingResult in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
print(response.result)
switch response.result
case .success:
let json = JSON(response.result.value!)
if(delegate != nil)
delegate!.didReceiveResult(json,basePath: basePath)
case .failure(let error):
print(error)
case .failure(let encodingError):
print(encodingError)
)
带有图像(来自参数转换的图像作为数据)。当我调试它时,我得到了 response.result 返回失败
【问题讨论】:
【参考方案1】:您没有上传剩余的两个参数content
和location_id
。
试试这个,看看结果。我也在这里使用了SwiftyJSON
。
这是我用于所有 API 处理的 APIManager
类。
import Alamofire
import SwiftyJSON
class APIManager: NSObject
class func apiMultipart(serviceName:String,parameters: [String:Any]?, completionHandler: @escaping (JSON?, NSError?) -> ())
Alamofire.upload(multipartFormData: (multipartFormData:MultipartFormData) in
for (key, value) in parameters!
if key == "imageName"
multipartFormData.append(
value as! Data,
withName: key,
fileName: "swift_file.jpg",
mimeType: "image/jpg"
)
else
//Data other than image
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
, usingThreshold: 1, to: serviceName, method: .post, headers: ["yourHeaderKey":"yourHeaderValue"]) (encodingResult:SessionManager.MultipartFormDataEncodingResult) in
switch encodingResult
case .success(let upload, _, _):
upload.responseJSON response in
if response.result.error != nil
completionHandler(nil,response.result.error as NSError?)
return
print(response.result.value!)
if let data = response.result.value
let json = JSON(data)
completionHandler(json,nil)
break
case .failure(let encodingError):
print(encodingError)
completionHandler(nil,encodingError as NSError?)
break
如果您没有任何标题,则可以将字段保留为 ["":""]
代替您的 ["yourHeaderKey":"yourHeaderValue"]
现在为了调用,我刚刚在控制器中形成了参数。
var params = [String:AnyObject]()
params["content"] = "something" as AnyObject?
params["location_id"] = "201" as AnyObject?
// Grab your image from some variable or imageView. Here self.profileImage is a UIImage object
if let profileImageData = self.profileImage
if let imageData = UIImageJPEGRepresentation(profileImageData, 0.5)
params["imageName"] = imageData as AnyObject?
APIManager.apiMultipart(serviceName: "http://yourAPIURL", parameters: params, completionHandler: (response:JSON?, error:NSError?) in
//Handle response
)
else
print("Image problem")
【讨论】:
是的,已解决,我错过了 2 个参数并对其进行了编码。非常感谢。以上是关于使用 Alamofire 将图像上传到 API 服务器的主要内容,如果未能解决你的问题,请参考以下文章
带有参数和标题的 Alamofire Multipart-form 图像上传 - Swift
使用 AlamoFire 和预签名 URL 将图像上传到 S3 存储桶时出现问题