上传图像到服务器使用Alamofire
这是我的代码,我想上传图像到服务器使用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)
}
})
试试下面的代码
let image = UIImage.init(named: "myImage")
let imgData = UIImageJPEGRepresentation(image!, 0.2)!
let parameters = ["name": rname]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
to:"mysite/upload.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
}
let params: Parameters = ["name": "abcd" "gender": "Male"]
Alamofire.upload(multipartFormData:
{
(multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.yourimageView.image!, 0.1)!, withName: "image", fileName: "file.jpeg", mimeType: "image/jpeg")
for (key, value) in params
{
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to:yourUrl,headers:nil)
{ (result) in
switch result {
case .success(let upload,_,_):
upload.uploadProgress(closure: { (progress) in
//Print progress
})
upload.responseJSON
{ response in
//print response.result
if response.result.value != nil
{
let dict :NSDictionary = response.result.value! as! NSDictionary
let status = dict.value(forKey: "status")as! String
if status=="1"
{
print("DATA UPLOAD SUCCESSFULLY")
}
}
}
case .failure(let encodingError):
break
}
}
你的答案在没有任何解释的情况下会有用吗? – Shashanth
只要确保参数中的所有值都是字符串,就会导致崩溃。 –
所以如果我有webservice的参数名称是徽标 我应该在哪里替换它 withName:“image”。 或 文件名:“file.jpeg” –
需要指定name, fileName, mimeType
,这些都是很重要的多台服务器
func upload(image: UIImage, completion: (URL?) -> Void) {
guard let data = UIImageJPEGRepresentation(image, 0.9) else {
return
}
Alamofire.upload(multipartFormData: { (form) in
form.append(data, withName: "file", fileName: "file.jpg", mimeType: "image/jpg")
}, to: "https://yourawesomebackend.com", encodingCompletion: { result in
switch result {
case .success(let upload, _, _):
upload.responseString { response in
print(response.value)
}
case .failure(let encodingError):
print(encodingError)
}
})
}
什么是'rname'这里?在'let参数= [“name”:rname]' –
不知道rname来自哪里。然而,看起来这个答案和教程非常相似[这里](https://www.raywenderlich.com/147086/alamofire-tutorial-getting-started-2) –
@JeffMuir本教程不包括这个 – iosMentalist