Upload files using multipart request - Swift 4

六月ゝ 毕业季﹏ 提交于 2019-12-06 07:11:37

Try with:

multipartFormData.append(fileData, withName: "file", fileName: "file", mimeType: "image/png")

I have created one function. Hope it works for you.

//Alamofire file upload code
func requestWith(URLString: String,
                 imageData: Data?,
                 fileName: String?,
                 pathExtension: String?,
                 parameters: [String : Any],
                 onView: UIView?,
                 vc: UIViewController,
                 completion:@escaping (Any?) -> Void,
                 failure: @escaping (Error?) -> Void) {

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

    let URL = BASE_PATH + URLString
    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: "fileUpload", fileName: "\(fileName!).\(pathExtension!)", mimeType: "\(fileName!)/\(pathExtension!)")
        }

    }, usingThreshold: UInt64.init(), to: URL, method: .post, headers: headers) { (result) in

        switch result {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                if let err = response.error {
                    failure(err)
                    return
                }
                completion(response.result.value)
            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")
            failure(error)
        }
    }
}

Try This, is an example it works for me. If you want to convert encode 64 for images add extension. If simply post the data and image this code may help.

//creating parameters for the post request

    let parameters: Parameters=[

        "ad_title":classText1.text!,
        "ad_description":classText2.text!,
        "ad_category":CategoryClass.text!,
        "ad_condition":classText3.text!,
        "ad_username":classText6.text!,
        "ad_usermobile":classText7.text!,
        "ad_city":classText8.text!,
        "ad_price":classText4.text!,
        "negotiable":classText5.text!,
        "image1":adImage1.image!,
        "image2":adImage2.image!,
        "image3":adImage3.image!,
        "image4":adImage4.image!


    ]
    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for (key, value) in parameters {
            multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
        }


    }, usingThreshold: UInt64.init(), to: URL, method: .post) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print("Succesfully uploaded  = \(response)")
                if let err = response.error{

                    print(err)
                    return
                }

            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")

        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!