0

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)")
            })
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Nouf
  • 733
  • 1
  • 11
  • 32
  • You should give some custom unique name to files so that you can fetch using `FileManager` from `document directory`. And you can check whether file exists already or not if you use `FileManager`. – Sharad Chauhan Apr 30 '19 at 09:13
  • if I took the path from DefaultDownloadResponse.destinationURL I will found my files in simulator , can I use it? – Nouf Apr 30 '19 at 09:45
  • 1
    yes, but better to give custom name also in almofire so that you can easily search for the particular file easily. – Sharad Chauhan Apr 30 '19 at 09:48
  • can you share me code , because I was not able to make it work? – Nouf May 01 '19 at 10:14
  • Add code what you have tried after getting some idea from my comment. – Sharad Chauhan May 03 '19 at 04:14

1 Answers1

1

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