![](/img/trans.png)
[英]Upload files with parameters from multipartformdata using alamofire 5 in ios swift
[英]Alamofire multipartFormData using [String : Any] parameters
您好,我正在嘗試發布參數和/或圖像。 我的參數是一個帶有字符串、日期、整數、值的 [String : Any]。 當我只發布一個參數時,我使用 URLEncoding.default 編碼。 但是,當我需要同時發布參數和圖像時,我使用 multipartFormData。 我的代碼在下面
if url == ""{
AF.upload(multipartFormData: { multipartFormData in
for (key,value) in parameters {
multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
let jpegData = art!.jpegData(compressionQuality: 1.0)
multipartFormData.append(Data((jpegData)!), withName: "photo")
}, to: "\(NetworkManager.rootURL)/api/add/")
.responseJSON { response in
debugPrint(response)
}
}else{
AF.request("\(NetworkManager.rootURL)/api/add/", method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).response { (reponse) in
let status = reponse.response!.statusCode
if status == 200{
completion(200)
}else{
completion(401)
}
}
}
我的問題是,因為我的參數是帶有字符串日期 int 值的 any,所以我得到了一個Could not cast value of type 'Swift.Int' (0x1c3f1f1e8) to 'Swift.String' (0x1c3f21390).
有什么解決方法嗎? 還是我必須將所有內容更改為字符串...
任何幫助表示贊賞
您需要將任何值作為String
路徑才能工作,因為這是將其傳輸到Data
的唯一方法,其他一些使用
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
我已經為使用 Multipart 將帶有參數的圖像上傳到服務器制作了塊功能
//Here strUrl = YOUR WEBSERVICE URL
//postParam = post Request parameter i.e.
//let postParam : [String : Any] = [first_name : "name"]
//imageArray = image upload array i.e.
//var imageArray : [[String:Data]] = [["image_name" : YOUR IMAGE DATA]]
func postImageRequestWithURL(withUrl strURL: String,withParam postParam: Dictionary<String, Any>,withImages imageArray:[[String:Data]], completion:@escaping (_ isSuccess: Bool, _ response:NSDictionary) -> Void)
{
let requetURL = strURL
Alamofire.upload(multipartFormData: { (MultipartFormData) in
for (imageDic) in imageArray
{
for (key,value) in imageDic
{
MultipartFormData.append(value, withName:key,fileName: "file.jpg", mimeType: "image/jpg")
}
}
for (key, value) in postParam
{
MultipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
// MultipartFormData.append(value, withName: key)
}
}, usingThreshold: UInt64.init(), to: requetURL, method: .post, headers: ["Accept": "application/json"]) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
let desiredString = NSString(data: response.data!, encoding: String.Encoding.utf8.rawValue)
print("Response ====================")
print(desiredString!)
if let json = response.result.value as? NSDictionary
{
if response.response?.statusCode == 200
|| response.response?.statusCode == 201
|| response.response?.statusCode == 202
{
completion(true,json);
}
else
{
completion(false,json);
}
}
else
{
completion(false,[:]);
}
}
case .failure(let encodingError):
print(encodingError)
completion(false,[:]);
}
}
}
您還可以使用此上傳多張圖片我希望這會有所幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.