作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在将 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)
}
最佳答案
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)
}
}
关于ios - 尝试通过 Alamofire 4.0 上传图像时,类型 "ParameterEncoding"没有成员 'URL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503040/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!