How to upload a file to Alamofire 4 and save it in the Documents folder?

I am trying to use Alamofire4 for Swift3 . I need to download mp3 files and save them in the Documents directory. The current code looks like this:

 func downloadAudioFromURL(url: String, completion: ((_ status: ResponseStatus, _ audioLocalURL: URL?) -> Void)?) { let fileManager = FileManager.default let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let audioFullURL = String.ensureFullURLPath(url) alamoManager.download(audioFullURL) .validate { request, response, temporaryURL, destinationURL in var pathComponent = response.suggestedFilename! if pathComponent == "m4a.mp4" { // Due to the old Android audio files without a filename // Assign a unique name so audio files don't overwrite each other pathComponent = "\(NSUUID().uuidString).mp4" } let localURL = directoryURL.appendingPathComponent(pathComponent) if response.statusCode == 200 { completion?(.success, localURL) } else { completion?(.failure, nil) } return .success } .responseJSON { response in debugPrint(response) print(response.temporaryURL) print(response.destinationURL) } } 

However, I cannot access the files from localURL after saving. I also noticed that localURL will be exactly the same for the different files I am trying to download (maybe they are overwritten?). For example: file:///Users/testuser/Library/Developer/CoreSimulator/Devices/D4254AEA-76DD-4F01-80AF-F1AF3BE8A204/data/Containers/Data/Application/29755154-DD21-4D4C-B340-6628607DC053/Documents/file1.mp3

Any ideas what I'm doing wrong here? Any pointers would be really appreciated! thanks!

Edited my code to look like this:

 func downloadAudioFromURL(url: String, completion: ((_ status: ResponseStatus, _ audioLocalURL: URL?) -> Void)?) { let destination: DownloadRequest.DownloadFileDestination = { _, _ in var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] documentsURL.appendPathComponent("Audiofile.mp3") return (documentsURL, [.removePreviousFile]) } Alamofire.download(url, to: destination).response { response in if let localURL = response.destinationURL { completion?(.success, localURL) } else { completion?(.failure, nil) } } } 

How can I check m4a.mp4 though?

+5
source share
2 answers

Why are you doing .validate ? You do not save data after loading in your current code. Alamofire allows you to store the file immediately after downloading:

 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 that you provide in the download method is the local URL in the Documents directory, not the server URL.

+6
source

Swift version 3.x and Alamofire 4.x

Well, of course, the Alamofire example posted by Alamofire itself has errors. Since fileURL returns Void , it cannot be used as a parameter in the return statement.

Also remove .createIntermediateDirectories from the options list of the return statement if you do not want any directories for the file you downloaded

EDIT
If you want to know the file type, just take the last component part and convert String to NSString as NSString to have these functions.

 //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

 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.

+4
source

Source: https://habr.com/ru/post/1257724/


All Articles