2016-12-01 86 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 !") 
      }