這是我的代碼,我想上傳圖像到服務器使用Alamofire,它不是錯誤,但它不能將圖像推送到服務器。我該怎麼辦 ?預先感謝。上傳圖像到服務器使用Alamofire
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)
}
})
什麼是'rname'這裏?在'let參數= [「name」:rname]' –
不知道rname來自哪裏。然而,看起來這個答案和教程非常相似[這裏](https://www.raywenderlich.com/147086/alamofire-tutorial-getting-started-2) –
@JeffMuir本教程不包括這個 – iosMentalist