2017-02-17 59 views
0

下面的代碼在Swift 3中完美地將上傳圖像文件作爲多部分。不過,我還沒有能夠獲得類似於Swift 2.2中的工作。如果我嘗試在Swift 2.2中使用它,我會收到消息Ambiguous reference to member 'upload(_:_:headers:file:)'模糊引用成員上傳(_:_:headers:file :)'

有沒有辦法在Swift 2.2中完成同樣的事情?我已經找到了幾個相關的問題,但只發現斯威夫特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) 
     } 
    } 

} 

回答

1

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

    } 
} 
+1

我認爲OP希望在雨燕2.2的解決方案... – hg8