1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| @objc func startUpload(param: [String: Any]) {
guard let header = param["header"] as? [String: Any] else { return } guard let formData = param["formData"] as?[String: Any] else { return }
if let urlString = param["url"] as? String, let filePath = param["filePath"] as? String, let autho = header["Authorization"] as? String, let client_id = header["client_id"] as? String, let client_secret = header["client_secret"] as? String, let fileName = formData["fileName"] as? String, let paramName = param["name"] as? String {
let fileUrl = URL.init(fileURLWithPath: filePath)
let boundary = "Boundary-\(UUID().uuidString)" let parameters = [ "fileName": fileName ]
let files = [ ( name: paramName, path: fileUrl.path ) ]
let url = URL(string: urlString)! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue(autho, forHTTPHeaderField: "Authorization") request.setValue(client_id, forHTTPHeaderField: "client_id") request.setValue(client_secret, forHTTPHeaderField: "client_secret") request.httpBody = try? createBody(with: parameters, files: files, boundary: boundary)
let session = URLSession.shared let uploadTask = session.dataTask(with: request, completionHandler: { (data, _, error) -> Void in if error != nil { print(error!) } else { let str = String(data: data!, encoding: String.Encoding.utf8) print("--- 上传完毕 ---\(str!)") } })
uploadTask.resume() } }
private func createBody(with parameters: [String: String]?, files: [(name: String, path: String)], boundary: String) throws -> Data { var body = Data() if parameters != nil { for (key, value) in parameters! { body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.append("\(value)\r\n") } }
for file in files { let url = URL(fileURLWithPath: file.path) let filename = url.lastPathComponent let data = try Data(contentsOf: url) let mimetype = mimeType(pathExtension: url.pathExtension)
body.append("--\(boundary)\r\n") body.append("Content-Disposition: form-data; " + "name=\"\(file.name)\"; filename=\"\(filename)\"\r\n") body.append("Content-Type: \(mimetype)\r\n\r\n") body.append(data) body.append("\r\n") }
body.append("--\(boundary)--\r\n") return body }
func mimeType(pathExtension: String) -> String { if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue() { if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)? .takeRetainedValue() { return mimetype as String } } return "application/octet-stream" } extension Data { mutating func append(_ string: String, using encoding: String.Encoding = .utf8) { if let data = string.data(using: encoding) { append(data) } } }
|