ios – 如何在Alamofire 4中下载文件并保存到Documents目录?

前端之家收集整理的这篇文章主要介绍了ios – 如何在Alamofire 4中下载文件并保存到Documents目录?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将Alamofire4用于 Swift3.我需要下载.mp3文件并将它们保存到Documents目录.目前的代码是这样的:
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)
        }
    }

但是,保存后我无法实际访问localURL中的文件.我还注意到,对于我尝试下载的不同文件,localURL将完全相同(也许它们会被覆盖?).例如:file:/// Users / testuser / Library / Developer / CoreSimulator / Devices / D4254AEA-76DD-4F01-80AF-F1AF3BE8A204 / data / Containers / Data / Application / 29755154-DD21-4D4C-B340-6628607DC053 / Documents / file1 .MP3

我在这里做错了什么想法?

编辑我的代码看起来像这样:

func downloadAudioFromURL(url: String,_ 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)
            }

        }
}

我怎么检查m4a.mp4呢?

解决方法

你为什么表演.validate?下载后,您不会在当前代码中存储任何数据.
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)
    }
}

顺便提一下,您在下载方法中提供的下载路径是Documents目录的本地URL,而不是服务器的URL.

原文链接:/iOS/332752.html

猜你在找的iOS相关文章