2016-12-01 63 views
0

我想從我的API下載文件中的數據(令牌需要),actualy,我有這樣的代碼:Alamofire - 下載文件

let headers = [ 
      "token": getToken() 
     ] 
Alamofire.download("http://API-URL.com/data/chapter/\(chapterID)/file", headers: headers) 
     .downloadProgress { progress in 
      print("Download Progress: \(progress.fractionCompleted)") 
     } 
     .responseData { response in 
      print(response.result.value) 
     } 

的文件下載,我可以看到它在tmp目錄模擬器, 進度事件被解僱過,但結局下載後,臨時文件被刪除,response.result.valuenil

我怎麼能在文件與頭的請求和事件進展下載數據?

編輯我:

response.result.error給我:

Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(文件:///用戶/ us001 /庫/開發/ CoreSimulator /設備/ FABD2181-9702-4599-8D2D-7B25E2128F4A /數據/容器/數據/應用/ C63C4524-6A53-410B-BE3E-66B5E6F6D069/TMP/CFNetworkDownload_zyr0xF.tmp

編輯II:

通過Alamofire.request改變Alamofire.download刪除錯誤,給我的數據大小response.result.value 但文件仍然被刪除

+0

它返回數據大小還是數據? –

回答

1

通過使用此代碼固定它:

Alamofire.request("http://elearning.api.univair-services.com/data/chapter/\(chapterID)/file", method: .get, headers: headers) 
     .downloadProgress { progress in 
      print("Download Progress: \(progress.fractionCompleted)") 
     } 
     .responseData { response in 
      //print(response.result.error) 
      do { 
       try response.result.value?.write(to: File.getPath(filename: "_cr")) 
      } catch { 
       print("oops !") 
      }