Browse Source

upload http

kevin 2 years ago
parent
commit
02b936c3f6
2 changed files with 45 additions and 1 deletions
  1. 2 1
      .gitignore
  2. 43 0
      appbuilder-ios/NexilisLite/NexilisLite/Source/Network.swift

+ 2 - 1
.gitignore

@@ -8,4 +8,5 @@ src/venv/
 .DS_Store
 Podfile.lock
 src/__pycache__/
-ExampleCode/
+ExampleCode/
+appbuilder-ios/NexilisLite/NexilisLite.xcodeproj/project.pbxproj

+ 43 - 0
appbuilder-ios/NexilisLite/NexilisLite/Source/Network.swift

@@ -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
     }