尝试通过 Alamofire 4.0 上传图像时,类型“ParameterEncoding”没有成员“URL”
Posted
技术标签:
【中文标题】尝试通过 Alamofire 4.0 上传图像时,类型“ParameterEncoding”没有成员“URL”【英文标题】:Type "ParameterEncoding" has no member 'URL' when trying to upload image through Alamofire 4.0 【发布时间】:2016-11-09 08:59:08 【问题描述】:在将 Alamofire 更新到 4.0 版以将图像上传到服务器之前,我使用相同的代码,它工作得非常好,但现在我遇到了这些错误,我正在尝试很多,但每次都会出现新错误:
第一个错误:
第二个错误:
我的代码:
func uploadFile()
let image = UIImage(named: "ios9.jpg")
let newimage : NSData = UIImageJPEGRepresentation(selectedImage.image!, 32)! as NSData
let newRandomName = randomStringWithLength(32)
let fname = (newRandomName as String) + ".jpg"
let parameters = [
"pic" :NetData(data: newimage as Data, mimeType: .ImageJpeg, filename: fname),
"msg" :self.detailmsg,
"customer_key" : self.CUSTOMER_KEY
] as [String : Any]
let urlRequest = self.urlRequestWithComponents("link", parameters: parameters as NSDictionary)
//First error here
Alamofire.upload(urlRequest.0, data: urlRequest.1)
.progress (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
print("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
.responseJSON response in
debugPrint(response)
func urlRequestWithComponents(_ urlString:String, parameters:NSDictionary) -> (URLRequestConvertible, NSData)
// create url request to send
let mutableURLRequest = NSMutableURLRequest(url: URL(string: urlString)!)
mutableURLRequest.httpMethod = Alamofire.HTTPMethod.post.rawValue
//let boundaryConstant = "myRandomBoundary12345"
let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
// create upload data to send
let uploadData = NSMutableData()
// add parameters
for (key, value) in parameters
uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)
if value is NetData
// add image
let postData = value as! NetData
//uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// append content disposition
let filenameClause = " filename=\"\(postData.filename)\""
let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"
let contentDispositionData = contentDispositionString.data(using: String.Encoding.utf8)
uploadData.append(contentDispositionData!)
// append content type
//uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) // mark this.
let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"
let contentTypeData = contentTypeString.data(using: String.Encoding.utf8)
uploadData.append(contentTypeData!)
uploadData.append(postData.data as Data)
else
uploadData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".data(using: String.Encoding.utf8)!)
uploadData.append("\r\n--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!)
// return URLRequestConvertible and NSData
// return (Alamofire.ParameterEncoding.encode(mutableURLRequest as! ParameterEncoding).0, uploadData)
//Second error here
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
【问题讨论】:
你可以通过multipartFormData
来完成这项工作
@HamzaAnsari 我该怎么做你能举个例子吗?
@samah 你是怎么解决的?有同样的问题 - 类型 ParameterEncoding 没有成员 'url'
@ThripthiHaridas 我可以通过将“URL”更改为小写“url”来消除错误
【参考方案1】:
let image = UIImage(named: "ios9.jpg")
let params:[String:Any] = ["msg": self.detailmsg,
"customer_key": self.CUSTOMER_KEY]
let urlRequest = try! URLRequest(url: url, method: .post, headers: nil)
Alamofire.upload(multipartFormData: (multipartFormData) in
if let imageData = UIImageJPEGRepresentation(image!, 1.0)
multipartFormData.append(imageData, withName: "pic", fileName:"image.jpg", mimeType: "image/jpg")
for (key, value) in self.parameters!
let data = "\(value)".data(using: .utf8)
multipartFormData.append(data!, withName: key)
, with: urlRequest) (result) in
switch result
case .success(let request, _, _):
request.responseJSON response in
debugPrint(response)
case .failure(let error):
print(error.localizedDescription)
【讨论】:
以上是关于尝试通过 Alamofire 4.0 上传图像时,类型“ParameterEncoding”没有成员“URL”的主要内容,如果未能解决你的问题,请参考以下文章
带有 MultiPart 表单数据中的参数的图像上传在 Alamofire 4.0 中不起作用
如何在 Alamofire 4.0 中添加带有上传进度百分比的标签的进度条
使用 Alamofire 从 UIImagePickerController 上传图像