How do I download a file in Alamofire 4 and save to the Documents directory?
Swift 3.x and Alamofire 4.x version
Well certainly the Alamofire
example posted by Alamofire itself has bugs. Since fileURL
returns Void
it can't be used as a parameter in return statement.
Also remove .createIntermediateDirectories
from option list of return statement if you don't want any Directories for the file you downloaded
EDIT
If you want to know the type of file, simply grab the last component part and convert String
to NSString
as NSString
have these functionalities.
//audioUrl should be of type URL
let audioFileName = String((audioUrl?.lastPathComponent)!) as NSString
//path extension will consist of the type of file it is, m4a or mp4
let pathExtension = audioFileName.pathExtension
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// the name of the file here I kept is yourFileName with appended extension
documentsURL.appendPathComponent("yourFileName."+pathExtension)
return (documentsURL, [.removePreviousFile])
}
Alamofire.download("yourAudioUrl", to: destination).response { response in
if response.destinationURL != nil {
print(response.destinationURL!)
}
}
Output is
file:///Users/rajan/Library/Developer/CoreSimulator/Devices/92B4AB6E-92C0-4864-916F-9CB8F9443014/data/Containers/Data/Application/781AA5AC-9BE7-46BB-8DD9-564BBB343F3B/Documents/yourFileName.mp3
which is the actual path of your file where it is stored.
Swift 5 - Alamofire 5.1. This is the approach.
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("image.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://httpbin.org/image/png", to: destination).response { response in
debugPrint(response)
if response.error == nil, let imagePath = response.fileURL?.path {
let image = UIImage(contentsOfFile: imagePath)
}
}
Why are you performing .validate
? You are not storing any data after download in your current code.
Alamofire allows you to store a file directly after download:
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendPathComponent("pig.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(urlString, to: destination).response { response in
print(response)
if response.result.isSuccess, let imagePath = response.destinationURL?.path {
let image = UIImage(contentsOfFile: imagePath)
}
}
And by the way, the download path you are providing in the download
method, is the local URL to the Documents directory and not a URL of a server.