Nouf
Nouf

Reputation: 773

Accessing Files from Alamofire Download Request

So I made a download request using Alamofire and this request and return image, voice, video and I was able to see the file in through the destinationURL but my question is how do I convert the request result to data I can use, like if I got image back how can add it to ImageView and so now, also I have one concern this function get called every time I open the page even if the file was download in the document, is not that going to take a lost of memory? and affect performance??

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

Upvotes: 0

Views: 2605

Answers (1)

Milad Moetamedi Nia
Milad Moetamedi Nia

Reputation: 105

You must do this to display the downloaded files!
You can go this way:

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
  }


}

and use:

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

Upvotes: 1

Related Questions