下面的代码在Swift 3中可以完美地将图像文件作为多部分上传。然而,我还没有得到任何类似的工作在斯威夫特2.2。如果我尝试在Swift 2.2中使用这个,就会得到消息Ambiguous reference to member 'upload(_:_:headers:file:)'
在Swift 2.2中有没有一种方法可以完成同样的任务?我发现了几个相关的问题,但只找到了在Swift 3中有效的解决方案。

func submitFile(entryId: Int, entryDetailValue: String, fieldId: Int, fieldType: String) {

    let parameters = [
        "entryId": "\(entryId)",
        "entryDetail": entryDetailValue,
        "fieldId": "\(fieldId)",
        "type": fieldType
    ]

    print(parameters)

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.imageView.image!, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
        }, to:"<my endpoint url>")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
                print(progress)
            })

            upload.responseData { response in
                print(response.result)
            }

        case .failure(let error):
            print(error)
        }
    }

}

最佳答案

在swift 3中试试这个

func uploadImageWithParameter()
{
    let id = String(describing: userDefaults.value(forKey: USER_LOGIN_ID)!)

    var parameters = [String:String]()
    parameters = ["title":headingTextfield.text!,
                  "id":id,
                  "description":descriptionTextview.text,
                  "author":authorTextfield.text!,
                  "url": urlTextfield.text!]

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(self.capturedImage, 0.5)!, withName: "pic", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }

    }, to:"your url")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (Progress) in
                print("Upload Progress: \(Progress.fractionCompleted)")
            })

            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()
                //self.removeImage("frame", fileExtension: "txt")
                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                }
            }

        case .failure(let encodingError):
            //self.delegate?.showFailAlert()
            print(encodingError)
        }

    }
}

10-04 16:31