除了@Fujia 的回复,我还想补充一个例子。这是我最近用来发布 png 文件的方法:
func WasperEntrepriseImageUploadCall(method: Alamofire.Method, imageData: NSData, parameters: [String: AnyObject]?, headers: [String: String]?, urlToPost: String,
progressionHandler: (bytesWritten: Int, totalBytesWritten: Int, totalBytesExpected: Int) -> (),
completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<AnyObject,NSError>, NSData?) -> ()){
Alamofire.upload(
method, urlToPost, headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "doesntmatter", mimeType: "image/png")
if let params = parameters{
for (key, value) in params {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
}
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
progressionHandler(bytesWritten: Int(bytesWritten), totalBytesWritten: Int(totalBytesWritten), totalBytesExpected: Int(totalBytesExpectedToWrite))
}
upload.response { response in
}.validate()
.responseJSON { response in
if let resp = response.response{
print(resp.statusCode)
print(response.result.value) // result of response serialization
}
completionHandler(response.request,response.response,response.result, response.data)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
}
不是最好的解决方案,但它应该让您领先一步。我相信同时需要 SwiftyJSON 和 Alamofire 3.0。为您提供一个可能有用的进度完成处理程序。您应该可以在这里找到更详细的答案:Uploading file with parameters using Alamofire