【问题标题】:Ambiguous reference to member 'upload(_:_:headers:file:)'对成员 'upload(_:_:headers:file:)' 的模糊引用
【发布时间】:2017-02-17 15:34:40
【问题描述】:

下面的代码在 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)
        }
    }

}

【问题讨论】:

    标签: ios swift swift2 alamofire multipart


    【解决方案1】:

    在 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)
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 2017-04-17
      • 2016-11-21
      • 1970-01-01
      • 2019-11-01
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多