You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.1 KiB
65 lines
2.1 KiB
//
|
|
// Created by Marco Schmickler on 21.03.15.
|
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
|
|
class UploadOperation: Operation {
|
|
let baseUrl: String
|
|
let data: Data
|
|
let path: String
|
|
|
|
init(baseUrl: String, data: Data, path: String) {
|
|
self.baseUrl = baseUrl
|
|
self.data = data
|
|
self.path = path
|
|
}
|
|
|
|
override func main() {
|
|
if self.isCancelled {
|
|
return
|
|
}
|
|
|
|
print("PATH: \(path)")
|
|
|
|
|
|
do {
|
|
try AF.upload(multipartFormData: { multipartFormData in
|
|
multipartFormData.append(self.data, withName: "file", fileName: self.path, mimeType: MimeType.Json.rawValue)
|
|
multipartFormData.append(self.path.data(using: String.Encoding.utf8)!, withName: "name")
|
|
}, with: URLRequest(url: URL(string: baseUrl)!, method: HTTPMethod.post))
|
|
.response(completionHandler: { data in
|
|
if let d = data.data {
|
|
print(String(decoding: d, as: UTF8.self))
|
|
}
|
|
})
|
|
} catch {
|
|
print("Upload Error")
|
|
};
|
|
// encodingCompletion: { encodingResult in
|
|
// switch encodingResult {
|
|
// case .success(let upload, _, _):
|
|
// upload.responseJSON { response in
|
|
// print(response.result.value)
|
|
// }
|
|
// case .failure(let error):
|
|
// print(error)
|
|
// }
|
|
// })
|
|
// .progress {
|
|
// (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
|
|
// print("progress : \(totalBytesWritten) / \(totalBytesExpectedToWrite)")
|
|
// }
|
|
// .response { (request, response, data, error) in
|
|
// if let d = data {
|
|
// let datastring = NSString(data: d, encoding:NSUTF8StringEncoding)
|
|
// print("d \(datastring)")
|
|
// }
|
|
// if let e = error {
|
|
// print("e \(e)")
|
|
// }
|
|
// }
|
|
}
|
|
}
|