Upload files with parameters from multipartformdata using alamofire 5 in ios swift


Question


I am trying upload files with parameters (multipartformdata) but i can't do it with new version Alamofire 5, if you have some experience with Alamofire 5 please share it with me.

 func uploadPluckImage(imgData : Data, imageColumnName : String,  url:String,httpmethod:HTTPMethod,completionHandler: @escaping (NSDictionary?, String?) -> ()){
    let token = UserDefaults.standard.string(forKey: PrefKeys.loginToken) ?? ""
    let authorization = ["Authorization" : "Bearer \(token)"]
    let parameters: Parameters?
    parameters = [
        "garbageCollector": 0,
        "stuff_uuid": "2b4b750a-f4a6-4d61-84ce-7c42b5c030ee",
        "delete_file" : ""
    ]
    let headers : HTTPHeader?
    headers = ["Authorization" : "Bearer \(token)"]
    let imageURl = "http://68.183.152.132/api/v1/stuff/uploader"


    AF.upload(multipartFormData: { (multipart: MultipartFormData) in
        let imageData = self.firstImage.image?.jpegData(compressionQuality: 0.7)
            multipart.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png")

        for (ker, value) in parameters!{
            multipart.append(value as! String).data(using: .utf8)!, withName: key)
        }
    },usingThreshold: UInt64.init(),
       to: imageURl,
       method: .post,
       headers: headers,
       encodingCompletion: { (result) in
        switch result {
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in
              print("Uploading")
            })
            break
        case .failure(let encodingError):
            print("err is \(encodingError)")
                break
            }
        })
}

Answer 1:


Upload method slightly changed in Alamofire 5

func upload(image: Data, to url: Alamofire.URLRequestConvertible, params: [String: Any]) {
    AF.upload(multipartFormData: { multiPart in
        for (key, value) in params {
            if let temp = value as? String {
                multiPart.append(temp.data(using: .utf8)!, withName: key)
            }
            if let temp = value as? Int {
                multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
            }
            if let temp = value as? NSArray {
                temp.forEach({ element in
                    let keyObj = key + "[]"
                    if let string = element as? String {
                        multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                    } else
                        if let num = element as? Int {
                            let value = "\(num)"
                            multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                    }
                })
            }
        }
        multiPart.append(image, withName: "file", fileName: "file.png", mimeType: "image/png")
    }, with: url)
        .uploadProgress(queue: .main, closure: { progress in
            //Current upload progress of file 
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .responseJSON(completionHandler: { data in
            //Do what ever you want to do with response
        })
}

Hope this will help you

EDIT: In case you don't quite get the above, here is an expansion:

let uploadRequest: UploadRequest = AF.upload(multipartFormData: multipartFormData, with: ...)
let completionHander: (AFDataResponse<Any>) -> Void) = { result in
    //Do what ever you want to do with response, which is a DataResponse<Success, AFError>
}
// Adds that completion hander to the UploadRequest
uploadRequest.responseJSON(completionHandler: completionHander)



Answer 2:


This how I upload images and videos from a swift 5 app with Alamofire 5.

Images

    /**
     Send Image to server
     */

    func Post(imageOrVideo : UIImage?){  

    let headers: HTTPHeaders = [
        /* "Authorization": "your_access_token",  in case you need authorization header */
        "Content-type": "multipart/form-data"
    ]


        AF.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "file.jpeg", mimeType: "image/jpeg")
        },
            to: "http://ip.here.--.--/new.php", method: .post , headers: headers)
            .response { resp in
                print(resp)               

        }
}

You can create a temporary resource and use the temporary url (good for videos):

/**
 Send video to server
 */
func PostVideoUrl(url : URL){

    let headers: HTTPHeaders = [
        "Content-type": "multipart/form-data"
    ]        

    AF.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(url, withName: "upload_data" , fileName: "movie.mp4", mimeType: "video/mp4")
    },
        to: "http://ip.here.--.--/newVideo.php", method: .post , headers: headers)
        .response { resp in
            print(resp)

    }

}



Answer 3:


This Is work for me Using Swift 4.2 Please try this

let url = "http://google.com" /* your API url */

let headers: HTTPHeaders = [
    /* "Authorization": "your_access_token",  in case you need authorization header */
    "Content-type": "multipart/form-data"
]

Alamofire.upload(multipartFormData: { (multipartFormData) in
    for (key, value) in parameters {
        multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
    }
    
    if let data = imageData{
        multipartFormData.append(data, withName: "image", fileName: "image.png", mimeType: "image/png")
    }
    
}, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
    switch result{
    case .success(let upload, _, _):
        upload.responseJSON { response in
            print("Succesfully uploaded")
            if let err = response.error{
                onError?(err)
                return
            }
            onCompletion?(nil)
        }
    case .failure(let error):
        print("Error in upload: \(error.localizedDescription)")
        onError?(error)
    }
}



Answer 4:



Upload two images with parameters with Headers , Using Alamofire 5

i solve this issue try this code ..

//MARK: Upload Images with Params API's

func upload(icon: Data, image : Data, params: [String: Any]) {
    let urlString = "Your URL String "
    let headers: HTTPHeaders =
        ["Content-type": "multipart/form-data",
        "Accept": "application/json"]
    AF.upload(
        multipartFormData: { multipartFormData in
            for (key, value) in params {
                if let temp = value as? String {
        multipartFormData.append(temp.data(using: .utf8)!, withName: key)}

    if let temp = value as? Int {
    multipartFormData.append("(temp)".data(using: .utf8)!, withName: key)}

    if let temp = value as? NSArray {
        temp.forEach({ element in
            let keyObj = key + "[]"
            if let string = element as? String {
                multipartFormData.append(string.data(using: .utf8)!, withName: keyObj)
            } else
                if let num = element as? Int {
                    let value = "(num)"
                    multipartFormData.append(value.data(using: .utf8)!, withName: keyObj)
            }
        })
    }
}
            multipartFormData.append(icon, withName: "icon", fileName: "icon.png", mimeType: "icon/png")

            multipartFormData.append(image, withName: "registerImage", fileName: "registerImage.png", mimeType: "registerImage/png")
    },
        to: urlString, //URL Here
        method: .post,
        headers: headers)
        .responseJSON { (resp) in
            defer{SVProgressHUD.dismiss()}
            print("resp is \(resp)")
    }
}## 
  1. Heading

    #




Answer 5:


You can do that using Router URLRequestConvertible

static func performMultiPartFile<T:Decodable>(imageData: Data, route:APIRoute,decoder: JSONDecoder = JSONDecoder(), completion: @escaping (_ result: T?, _ error: String?) -> ()) {
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(imageData, withName: "image", fileName: "iosImage.jpg", mimeType: "image/jpg")
    }, with: route) { (encodingResult) in
        switch encodingResult {

        case .success(let upload, _, _):
            upload.responseString { (response) in
                if response.result.isSuccess {
                    if let JSON = response.result.value {

                        debugPrint("✅ Respons Object >>>> " + String(describing: JSON))
                        do {
                            let result = try JSONDecoder().decode(T.self, from: JSON.data(using: .utf8)!)
                            debugPrint("✍️ Result: " + String(describing: result))
                            completion(result, nil)
                        } catch let error { // mapping fail
                            debugPrint("❌ Error in Mapping" + String(describing: error))
                            completion(nil, String(describing: error))
                        }
                    }
                } else {
                    debugPrint("❌ 					



					
										
					
								

码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者爷万人跪拜码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者爷万人跪拜码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: