从Alamofire下载请求访问文件

时间:2019-04-30 08:57:58

标签: ios swift alamofire alamofire-request

因此,我使用Alamofire发出了下载请求,并且该请求并返回图像,语音,视频,并且能够通过destinationURL看到文件,但是我的问题是如何将请求结果转换为数据我可以使用,就像我将图像取回来一样如何将其添加到ImageView中,所以现在,我也担心每次打开页面时都会调用此函数,即使文件已在文档中下载,也不会失去记忆?并影响性能??

        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
        Alamofire.download(
            "url",
            method: .get,
            parameters: nil,
            encoding: JSONEncoding.default,
            headers:nil ,
            to: destination).downloadProgress(closure: { (progress) in
                //progress closure
            }).response(completionHandler: { (DefaultDownloadResponse) in
                //here you able to access the DefaultDownloadResponse
                //result closure
                print("*****DefaultDownloadResponse***** \(DefaultDownloadResponse.response) and \(DefaultDownloadResponse.resumeData) and \(DefaultDownloadResponse.destinationURL)")
            })

1 个答案:

答案 0 :(得分:0)

您必须执行此操作才能显示下载的文件!
您可以这样:

extension ViewPreRegistrationViewController : UIDocumentInteractionControllerDelegate {


  func startDownload(Url:String) -> Void {
    headers = ["Authorization": "Token \(Auth.userToken!)"]
    let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
    Alamofire.download(
      Url,
      method: .get,
      encoding: JSONEncoding.default,
      headers: headers,
      to: destination).downloadProgress(closure: { (progress) in
        //progress closure
      }).response(completionHandler: { (DefaultDownloadResponse) in
        //here you able to access the DefaultDownloadResponse
        let docController = UIDocumentInteractionController(url: DefaultDownloadResponse.destinationURL!)
        docController.delegate = self
        docController.name = "show"
        docController.presentPreview(animated: true)
        //result closure
      })
  }



  func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
    return self
  }


}

并使用:

  @IBAction func exportPdfButton(_ sender: Any) {
    let fullURL = "your url"
    startDownload(Url: fullURL)
  }