this is my code that I want to upload image to server using Alamofire, it not error but it can't push image to server. what should I do?
let url = URL(string: urlString)!
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
let parameters = ["name": rname]
do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
} catch {
print(error)
}
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let image = UIImage.init(named: "myImage")
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
Alamofire.upload(multipartFormData: { MultipartFormData in
MultipartFormData.append(imgData, withName: "fileset", fileName: "name", mimeType: "image/jpg")
},with: urlRequest,encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if let info = response.result.value as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
print("LINK: (imgLink)")
}
}
}
} case .failure(let error):
print(error)
}
})
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…