我正在尝试将数据上传到我们的服务器。数据是转换为 .zip 的 zip 文件Data
。
SSZipArchive.createZipFile(atPath: "\(self.currentWPDirectory!)/\(self.documentId!).zip", withFilesAtPaths: checkInFileList)
let zipData = try! Data(contentsOf: URL(string: "file:///\(self.currentWPDirectory!)/\(self.documentId!).zip")!)
网址会话
let parameters = "?documentId=\(self.documentId!)&documentAction=CHECKIN&doc_action_comment=some comment&device_name=\(UIDevice.current.name)"
let urlString = "\(Constants.WebService.checkIn)\(parameters)"
let url = urlString.replacingOccurrences(of: " ", with: "%20")
let request = NSMutableURLRequest(url: URL(string: url)!)
let data = zipData
request.httpMethod = HTTPMethod.post.rawValue
let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let contentType = "multipart/form-data; boundary=\(boundary)"
request.setValue(contentType, forHTTPHeaderField:"Content-Type")
let body = NSMutableData();
let tempData = NSMutableData()
let fileName = "FILE_NAME" + ".zip"
let parameterName = "userfile"
let mimeType = "application/octet-stream"
tempData.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
let fileNameContentDisposition = fileName != "" ? "filename=\"\(fileName)\"" : ""
let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\"; \(fileNameContentDisposition)\r\n"
tempData.append(contentDisposition.data(using: String.Encoding.utf8)!)
tempData.append("Content-Type: \(mimeType)\r\n\r\n".data(using: String.Encoding.utf8)!)
tempData.append(zipData)
tempData.append("\r\n".data(using: String.Encoding.utf8)!)
body.append(tempData as Data)
body.append("\r\n--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request.httpBody = body as Data
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.uploadTask(with: request as URLRequest, from: data) { (data, response, err) in
// Gets 206 in response
}
task.resume()
阿拉莫菲尔
let params = [
"documentId": self.documentId!,
"documentAction": (self.isSync) ? "SYNC" : "CHECKIN",
"doc_action_comment": "Some Comment",
"device_name": UIDevice.current.name
]
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(zipData, withName: "FILE_DATA", fileName: "\(self.documentId!).zip", mimeType: "application/zip")
for (key, value) in params {
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
},
to: "\(Constants.WebService.checkIn)?documentId=\(self.documentId!)&documentAction=\((self.isSync) ? "SYNC" : "CHECKIN")&doc_action_comment=comment&device_name=\(UIDevice.current.name)",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response)
// Get 406 Response code
// Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0."
}
self.completionCallback!(true)
}
case .failure(let encodingError):
print(encodingError)
self.completionCallback!(false)
}
}
)
我尝试了很多方法来获取多部分表单/数据上传以发送我的数据,但没有成功。谁能指出我正确的方向?