我有一个正在运行的项目,该项目使用Alamofire处理所有请求,包括带有分段数据的上传。目前,此方法工作正常,但是我们希望转而使用GZIP处理所有请求和响应。我正在使用gzip swift库,现在可以通过请求接收gzip压缩的数据,但是我不知道如何对导致调用失败的多部分数据进行gzip编码。
我一直在寻找解决方案,尽管我遇到了一篇有关该解决方案的文章,但提供的示例仅适用于较早的Alamofire和Swift版本。我尝试破解它以使其正常工作,但是有一些我无法解决的错误。
我的请求代码如下:
self.sessionManager.upload(
multipartFormData: { multipartFormData in
multipartFormData.append("\(sUUID)".data(using: String.Encoding.utf8)!, withName: "sUUID")
multipartFormData.append(sStream, withName: "files[]", fileName: fileName, mimeType: "image/\(sExt)")
multipartFormData.append("\(convertedEventID)".data(using: String.Encoding.utf8)!, withName: "nEventID")
multipartFormData.append("\(sExt)".data(using: String.Encoding.utf8)!, withName: "sExt")
multipartFormData.append("0".data(using: String.Encoding.utf8)!, withName: "bRecurring")
multipartFormData.append("\(fileName)".data(using: String.Encoding.utf8)!, withName: "sDescription")
multipartFormData.append("File Attached By User".data(using: String.Encoding.utf8)!, withName: "sWordDocumentType")
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sWordDocumentName")
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sSname")
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sEmailRecip")
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sEmailSender")
multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sNotes")
multipartFormData.append("ME".data(using: String.Encoding.utf8)!, withName: "sAgent")
},
to: "/Upload",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseString { response in
let decompressedData: Data
if response.data!.isGzipped {
decompressedData = try! response.data!.gunzipped()
} else {
decompressedData = response.data!
}
NotificationCenter.default.post(name: .didReceiveData, object: self, userInfo: ["pass" : 1])
completion(true, 200, decompressedData, nil)
}
upload.uploadProgress { progress in
print(progress.fractionCompleted)
}
case .failure(let encodingError):
print(encodingError)
completion(false, 404, nil, encodingError)
}
})
答案 0 :(得分:0)
如果您需要进行gzip编码之类的后期处理,则将无法使用内置的public class Employee {
//@SheetColumn("Name")
private String name;
//@SheetColumn("Age")
private Integer age;
private Address address;
}
方法。相反,您需要分别使用Alamofire的多部分编码,生成数据,将其压缩,然后使用Alamofire上载。
此外,您无需手动解压缩响应。只要相应的upload(multipartFormData:...)
标头再次出现,表明已将其压缩,应为您自动将其解压缩。
最后,您可以使用Content-Encoding
从Data
s中轻松创建String
值。