let formData1 = MultipartFormData(provider: .data((parameters["name"] as! String).data(using: .utf8)!), name: "name")
let formData2 = MultipartFormData(provider: .data((parameters["place"] as! String).data(using: .utf8)!), name: "place")
let formData3 = MultipartFormData(provider: .data((parameters["desc"] as! String).data(using: .utf8)!), name: "desc")
let formData4 = MultipartFormData(provider: .data((String(parameters["holdNums"] as! Int)).data(using: .utf8)!), name: "holdNums")
let formData5 = MultipartFormData(provider: .data((parameters["type"] as! String).data(using: .utf8)!), name: "type")
let formData6 = MultipartFormData(provider: .data(parameters["image"] as! Data), name: "file", fileName: "files.png", mimeType: "image/png")
var multipartData = [formData1,formData2,formData3,formData4,formData5,formData6]
if (parameters["approve"] as! Bool) == true {
multipartData.append(MultipartFormData(provider: .data(String((parameters["approve"] as! Bool)).data(using: .utf8)!), name: "approve"))
}
if (parameters["devices"] as! NSArray).count != 0 {
multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["devices"] as! [String] as NSArray)).data(using: .utf8)!), name: "devices"))
}
if (parameters["openUsers"] as! NSArray).count != 0 {
multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["openUsers"] as! [String] as NSArray)).data(using: .utf8)!), name: "openUsers"))
}
if (parameters["openDep"] as! NSArray).count != 0 {
multipartData.append(MultipartFormData(provider: .data((getJSONStringFromArray(array: parameters["openDep"] as! [String] as NSArray)).data(using: .utf8)!), name: "openDep"))
}
if (parameters["firstVerifier"] as! String) != "" {
multipartData.append(MultipartFormData(provider: .data((parameters["firstVerifier"] as! String).data(using: .utf8)!), name: "firstVerifier"))
}
if (parameters["secondVerifier"] as! String) != "" {
multipartData.append(MultipartFormData(provider: .data((parameters["secondVerifier"] as! String).data(using: .utf8)!), name: "secondVerifier"))
}
return .uploadMultipart(multipartData)
func getJSONStringFromArray(array: NSArray) -> String {
guard JSONSerialization.isValidJSONObject(array) else {
return ""
}
let data: NSData! = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData
let JSONString = NSString(data: data as Data,encoding: String.Encoding.utf8.rawValue)
return JSONString! as String
}
网友评论