微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/iOS/332752.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐