2016-09-20 128 views
42

由於遷移到Swift 3,我發現很難編譯使用Alamofire的項目。Swift 3 Alamofire分段上傳

Alamofire.upload(.POST, URL, headers: headers, multipartFormData: { 
     multipartFormData in 
. 
. 
. 
}) 

歧義參考構件:

問題上傳multipartFormData時,會發生 '上傳(_:向:方法:頭:)'

任何幫助非常理解的,在由於提前!

解決:

Alamofire.upload(multipartFormData: { (multipartFormData) in 

     multipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain") 


     for (key, value) in self.parameters { 
      multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) 
     } 
     }, with: URL2, encodingCompletion: { (result) in 

      switch result { 
      case .success(let upload, _, _): 

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

    }) 

這是如何上傳方法應在夫特3

+1

方法簽名已更改。評論你現有的並讓Xcode的自動完成指導你編寫新的。 – Moritz

+1

,你也可以去gitub上alamofire。 https://github.com/Alamofire/Alamofire – Sahil

回答

35

例如被實現,在使用Alamofire 4.0.0夫特3

(確保你已經準備好了4.0.0,因爲看起來你沒有更新ated您Alamofire還)

Alamofire.upload(multipartFormData: { (multipartFormData) in 
     // code 
    }, to: URL, encodingCompletion: { (result) in 
     // code 
    }) 

Alamofire.upload(multipartFormData: { (multipartFormData) in 
     // code 
    }, with: URL, encodingCompletion: { (result) in 
     // code 
    }) 

所以headers需要通過URL請求傳遞:

let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers) 
+0

另外一個問題,第一個佔位符(multipartFormData)中應該去什麼代碼以及後者應該去什麼(結果)?以前只有一個關閉 – DCDC

+0

它不是強制性的填充代碼,關閉是爲您最終使用。但是我猜想,完成關閉是非常有用的,因爲它是異步方法,因此您可以獲得反饋。 – pedrouan

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/123785/discussion-between-dcdc-and-pedrouan)。 – DCDC

6

試試這個和URL設置爲@pedrouan說。

Alamofire.upload(multipartFormData: { (multipartFormData) in 
     multipartFormData.append(imageData, withName: "xyz", fileName: "file.jpeg", mimeType: "image/jpeg") 
}, to: url) 
{ (result) in 
     //result 
} 
1

在swift 3中,試圖將multipartFormData設置爲@DCDC在他的解決方案中指出的。 XCode的嘗試。數據()之前投給AnyObject,所以不是

value.data(using: String.Encoding.utf8)!, withName: key 

我做

[replace_your_var_name_here].data(using: String.Encoding.utf8)!, withName: key 

在我的情況,我的VAR清單並非那麼大的硬編碼是一種選擇。

0

對於斯威夫特3和Alamofire〜4.3.0

如果有人像我試圖讓請求對象同步(不使用鎖或dispatch_groups),你可以使用這種方法:

// outer function 
... 
let string = "string to send" 
let multipartFormData = MultipartFormData() 
multipartFormData.append(string.data(using: .utf8)!, withName: "str") 

guard let data = try? multipartFormData.encode() else { 
    // fail appropriately 
} 

let request = sessionManager.upload(data, 
            to: url, 
            method: .post, 
/* this is VERY IMPORTANT LINE */ headers: ["Content-Type" : multipartFormData.contentType]) 

request.validate() 
// do whatever you need with request 

請注意,您需要設置Content-Type標題爲multipartFormData,因爲它包含邊界。

如果沒有需要有你的請求對象同步與

Alamofire.upload(multipartFormData: { (multipartFormData) in 

正在按預期對方的回答。在成功編碼數據的情況下,它將返回您在回調關閉中的請求對象。

重要提示:如果您使用我描述的方法,它會阻止您的線程(在大多數情況下,您可能在主線程中)複製和編碼您的數據。所以不要把它用於大文件或其他任何東西。在Alamofire中是異步的。

+0

'sessionManager'從哪裏來? – Arbitur

+0

@Arbitur它是您用來提出請求的Alamofire.SessionManager的一個實例。在我的生產代碼中,它是在應用程序初始化並在整個應用程序中使用時創建的。在答案的這個特定片段中,可以在//外部函數之後的標記爲'...'的代碼中創建它。雖然再次,這是不相關的,你已經創建了這個片段的會話管理器。 – user1264176