【问题标题】:Check if a file already exists before downloading it with Alamofire + suggestedDownloadDestination在使用 Alamofire +SuggestedDownloadDestination 下载文件之前检查文件是否已经存在
【发布时间】:2016-05-19 15:32:11
【问题描述】:

如何在使用 Alamofire 重新下载之前检查给定文件是否已经下载?我正在使用suggestedDownloadDestination,所以Alamofire 会自动选择文件名并将其保存在选择的目录中,例如.CachesDirectory。问题是suggestedDownloadDestination 给出的值是DownloadFileDestination 类型,只有通过使用请求的response 调用他才会返回NSURL,但这样我就永远无法知道文件路径而不执行之前的请求。

这是我目前使用 Alamofire 下载文件的代码:

Alamofire.download(.GET, downloadLink, destination: destination).progress {
                    bytesRead, totalBytesRead, totalBytesExpectedToRead in

                    }.response {
                        request, response, data, error in

                        guard error == nil else {
                            return
                        }

                        // This will give me the file path but we're already in a Request!
                        print("\(destination(NSURL(string: "")!, response!))")
                }

我错过了什么?

【问题讨论】:

  • 你找到解决办法了吗?
  • @osrl 不幸的是,Alamofire 并没有让您有机会轻松地做到这一点,因为您会使用建议的DownloadDestination,我使用下载目录的自定义路径进行了修复并删除了建议的DownloadDestination
  • 我尝试了.head的方法找出目标文件路径,并在下载文件之前检查它是否存在。但是即使下载请求方法是.head,Alamofire 也会创建文件。所以这也没有用。

标签: ios swift alamofire


【解决方案1】:

不确定您是否已经解决了这个问题,但您可以在 Alamofire.DownloadRequest 上创建一个扩展,例如:

extension Alamofire.DownloadRequest {
    open class func suggestedDownloadDestination(
        for directory: FileManager.SearchPathDirectory = .documentDirectory,
        in domain: FileManager.SearchPathDomainMask = .userDomainMask,
        with options: DownloadOptions)
        -> DownloadFileDestination
    {
        return { temporaryURL, response in
                let destination = DownloadRequest.suggestedDownloadDestination(for: directory, in: domain)(temporaryURL, response)
            return (destination.destinationURL, options)
        }
    }
}

现在您可以在选项参数中指定是否要覆盖文件:

let destination =  DownloadRequest.suggestedDownloadDestination(for: .cachesDirectory,
                                                                in: .userDomainMask,
                                                                with: [DownloadRequest.DownloadOptions.removePreviousFile])

【讨论】:

  • 1.) 问题是 - 如何知道文件已经存在/下载,而不是如何处理这种情况.... 2.) 如果文件已经存在,则不应再次下载...就我而言...
【解决方案2】:

这是我使用的解决方案

  1. 下载文件
  2. 检查是否存在,否则返回路径
  func downloadDocumentFile(filePath: String,onDownloadProgress: @escaping(_ progress: Double) -> Void,onError: @escaping(_ errorMessage: String) -> Void,onSuccess: @escaping(_ destinationUrl: URL) -> Void){
        guard let url = URL(string: filePath) else {
            onError("Couldn't create url from passed file path")
            assertionFailure()
            return
        }
        let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory, in: .userDomainMask)
        Alamofire.download(url, to: destination)
            .downloadProgress { (progress) in
                onDownloadProgress(progress.fractionCompleted)
        }
        .responseData(queue: .main) { (response) in
            switch response.result {
            case .success:
                if let destinationUrl = response.destinationURL {
                    onSuccess(destinationUrl)
                }else {
                    onError("Couldn't get destination url")
                    assertionFailure()
                }
            case .failure(let error):
                  // check if file exists before
                if let destinationURL = response.destinationURL {
                    if FileManager.default.fileExists(atPath: destinationURL.path){
                        // File exists, so no need to override it. simply return the path.
                        onSuccess(destinationURL)
                        print()
                    }else {
                        onError(error.localizedDescription)
                        assertionFailure()
                    }
                }else {
                    onError(error.localizedDescription)
                    assertionFailure()
                }
            }

        }
    }

【讨论】:

    猜你喜欢
    • 2013-12-21
    • 2018-06-21
    • 2019-03-07
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    相关资源
    最近更新 更多