我所做的只是:
func postMultipart(_ url: URL, parameters: [String: Any]?, headers: [String: String]?, image: UIImage?, success: @escaping SuccessHandler, failure: @escaping ErrorHandler) {
manager.upload(multipartFormData: { multipart in
if let image = image, let data = UIImagePNGRepresentation(image) {
multipart.append(data, withName: "Passport", fileName: "picture.png", mimeType: "image/png")
}
if let parameters = parameters {
for (key, value) in parameters {
multipart.append(self.archiver.data(from: value), withName: key)
}
}
}, to: url, method: .post, headers: headers) { result in
//parse result here
}
}
这是我看到的多部分发送到服务器的结果:
你可能会发现这是错的。它应该如下所示:
PMSReservationID: "23".
为什么它不是字符串,而是一些归档对象?
你可能需要它
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}