|
@@ -7,6 +7,7 @@
|
|
|
//
|
|
|
|
|
|
import Foundation
|
|
|
+import Alamofire
|
|
|
|
|
|
public class Network {
|
|
|
let uploadGroup = DispatchGroup()
|
|
@@ -182,6 +183,48 @@ public class Network {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public func uploadHTTP(_ endUrl: String, files: [URL] = [], filename: [String] = [], parameters: [String : Any] = [:], onCompletion: (([String : Any]) -> Void)? = nil, onError: (() -> Void)? = nil, onProgress: ((Progress) -> Void)? = nil){
|
|
|
+
|
|
|
+ guard filename.isEmpty else {
|
|
|
+ let fileManager = FileManager.default
|
|
|
+ let documentDir = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
|
+ for name in filename {
|
|
|
+ let fileDir = documentDir.appendingPathComponent(name)
|
|
|
+ files.append(fileDir)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ AF.upload(multipartFormData: { (multipartFormData: MultipartFormData) in
|
|
|
+ for (key, value) in parameters {
|
|
|
+ multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
|
|
|
+ print(multipartFormData)
|
|
|
+ }
|
|
|
+
|
|
|
+ for i in 0..<files.count {
|
|
|
+ multipartFormData.append(files[i], withName: "file\(i+1)")
|
|
|
+ print(multipartFormData)
|
|
|
+ }
|
|
|
+
|
|
|
+ }, to: endUrl)
|
|
|
+ .responseJSON { result in
|
|
|
+ if let successResponse = result.value as? [String:Any] {
|
|
|
+ print("Response success")
|
|
|
+ onCompletion?(successResponse)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let statusCode = result.response?.statusCode
|
|
|
+ print("Response fail: \(statusCode)")
|
|
|
+ onError?()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .uploadProgress { progress in
|
|
|
+ if isCancel {
|
|
|
+ progress
|
|
|
+ }
|
|
|
+ onProgress?(progress)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public func cancel() {
|
|
|
self.isCancel = true
|
|
|
}
|