我在将Alamofire更新到版本4.0之前使用相同的代码将图像上传到服务器并且它工作得非常好但是现在我有这些错误我正在尝试很多,但每次出现新错误时:< / p>
我的代码:
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)
}
答案 0 :(得分:0)
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)
}
}