我很难获得正确的 Alamofire 语法来上传文件。服务器接口(interface)是一个 Rest API。我需要传递一些参数才能找到正确的数据库和特定记录。 示例:
"https://ram23jtog.tt.rp.com/ewws/EWUpdate/.json?$KB=ITIL&$table=asset_table&$login=testuser2&$password=pwd123&$lang=en&id=18005&tdd_form=2017-5-8-911-6633442.pdf"
关于参数需要注意的一件事是“ID”参数需要接近末尾,以便首先读取表和登录信息 - 或者它会出现。
我尝试加载参数然后稍后附加它们:
let parameters: Parameters = [
"&tdd_form" : "test",
"&id" : "\(self.inventoryDeviceVal.recId!)",
"$lang" : "en",
"$password" : "\(self.sharedDataVal.appUserPassword!)",
"$login" : "\(self.sharedDataVal.appUserID!)",
"$table" : "asset_table",
"$KB" : "ITIL"
]
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
在我看来,参数被截断了,或者存在阻止加载其中一些参数的错误。
Alamofire 请求:
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
multipartFormData.append(fileURL, withName: fileName, fileName: fileName, mimeType: "application/pdf")
}, to: updateCriteria) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request!) // original URL request
print(response.response!) // URL response
print(response.data!) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
}
有什么建议吗?做一个简单的 Alamofire 请求而不是分段上传会更容易吗?
决议
根据 Musa 的建议重新配置了我的参数:
let parameters = [
[
"name" : "$KB",
"value" : "ITIL"
],
[
"name" : "$table",
"value" : "asset_table"
],
[
"name" : "$login",
"value" : "\(self.sharedDataVal.appUserID!)"
],
[
"name" : "$password",
"value" : "\(self.sharedDataVal.appUserPassword!)"
],
[
"name" : "$lang",
"value" : "en"
],
[
"name" : "id",
"value" : "\(self.inventoryDeviceVal.recId!)"
]
]
我在参数定义中设置了参数“tdd_form”。我调整了追加,以便在正确传递参数的情况下使用右侧部分中的“tdd_form”参数对其进行正确编码。
Alamofire.upload(
multipartFormData: { multipartFormData in
for param in parameters {
multipartFormData.append((param["value"]?.data(using: .utf8)!)!, withName: param["name"]!)
}
multipartFormData.append(fileURL, withName: "tdd_form", fileName: fileName, mimeType: "application/pdf")
}, to: updateCriteria) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request!) // original URL request
print(response.response!) // URL response
print(response.data!) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
完美运行!!
最佳答案
我遇到了同样的问题,我是这样解决的
var parameters = [
[
"name": "tdd_form",
"value": "test"
],
[
"name": "id",
"value": "\(self.inventoryDeviceVal.recId!)"
]
.
.
.
etc
]
Alamofire.upload(
multipartFormData: { multipartFormData in
//convert parameters to data
for param in parameters {
multipartFormData.append((param["value"]?.data(using: .utf8)!)!, withName: param["name"]!)
}
//Add the data
//Mine was image , this is how I did it
//let uuid = UUID().uuidString.lowercased()
//multipartFormData.append(imgData, withName: "images[]", fileName: "\(uuid).jpg", mimeType: "image/jpeg")
multipartFormData.append(fileURL, withName: fileName, fileName: fileName, mimeType: "application/pdf")
},
to: updateCriteria){ (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request!) // original URL request
print(response.response!) // URL response
print(response.data!) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
关于ios - 使用 Swift 3 和 Alamofire 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43851834/