thetipsyhacker
thetipsyhacker

Reputation: 1422

Alamofire Not Downloading PDFs

I am trying to download PDFs to the documents directory. The request is successful, but the file doesn't exist in the documents directory after the request is finished.

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

let documentsDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentsDirectory, userDomainMask, true)

if paths.first != nil 
{

    Alamofire.download("http://www.test.com/pdfs/sample.pdf", to: destination).responseData 
    { response in
        if response.result.isFailure
        {
            print("\(response.error!)")
        }
    }
}

Upvotes: 1

Views: 202

Answers (1)

hasan
hasan

Reputation: 24205

The following works with me:

let fileExtension: String = fileUrl!.pathExtension
let documentURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filePath = documentURL.appendingPathComponent("\(disk_file_name!).\(fileExtension)")

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
     return (filePath, [.removePreviousFile])
}

Alamofire.download(URL(string: fileUrl)!, to: destination) ...

Upvotes: 1

Related Questions