我正在尝试将zipfile上传到服务器。当我尝试将zip文件上传到服务器时,文件未成功上传。我从服务器收到错误。在服务器端,userid与文件名分开。
实际的服务器JSON响应是:
{"error":{"login":"Sorry,Failed to Login "}}
我是multipart/form-data
的新手,所以我不知道这个问题背后的原因。
这是我的代码:
func uploadData() {
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
var username:String = username_textfield.text!
var password:String = password_textfield.text!
let fileManager = FileManager()
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("/cropsapdb_up_\(useridsaved).zip")
var filepath = paths
var sourceURL = URL(fileURLWithPath: filepath)
var zipnewdata:NSData = NSData()
zipnewdata = FileManager.default.contents(atPath: filepath) as! NSData
let parameters = ["unm2": username,"pass2":password,"ufile":filepath]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append( zipData, withName: "cropsapdb_up_\(self.useridsaved)", fileName: "cropsapdb_up_\(self.useridsaved).zip", mimeType: "application/zip")
for (key, value) in parameters {
multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
}
}, usingThreshold: UInt64.init(), to: "myurl", method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded = \(response.data)")
if let JSON = response.result.value
{
print("JSON: \(JSON)")
}
if let err = response.error{
print("response error")
return
}
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
}