ios - 对成员 'upload(_:_:headers:file:)' 的模糊引用

标签 ios swift swift2 alamofire multipart

下面的代码在 Swift 3 中完美地工作,可以将图像文件作为多部分上传。但是,我无法获得任何类似于在 Swift 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)
        }

    }
}

关于ios - 对成员 'upload(_:_:headers:file:)' 的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42301765/

相关文章:

php - 在 Laravel 或 iOS 应用程序中转换 JSON API 数据

ios - 如果 _ 是 _ 或 .isKind(of : ),正确的使用方法是什么

swift - UnsafeRawPointer 假设MemoryBound 与 bindMemory

ios - Swift - 使用 for 循环将数据添加到核心数据

ios - 个人热点 Swift 的多点连接

swift - 如何关闭 Scene Kit 中的环境光(使用 Swift)?

swift - Swift 中不区分大小写的字典

swift - 通过 SKAction 但不是通过 AVAudioPlayer 播放声音

xcode - 无法加载 xib 文件

ios - 在带有 Ludei/Cocoon JS 的 IOS 设备上使用 WebGL 不接触 ThreeJS OrbitControls