Swift5.0 Alamofire download的一般使用

Alamofire download

  1. 指定自己想要的文件名:
    就要重寫:DownloadRequest.DownloadFileDestination block
    具體代碼如下:
 let destination: DownloadRequest.DownloadFileDestination = {[weak self] (url, response) in
            let docmentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first//文件路徑
            //response.suggestedFilename!:源文件名包括擴展eg:demo.xls
            let suggestFil = response.suggestedFilename == nil ? "http.xls" : response.suggestedFilename!
            //新文件名
            let newName = self!.fileName + "." + suggestFil.split(separator: ".").last!.description
            let fileRUL = docmentURL?.appendingPathComponent(newName)//拼接處完整的路徑
            return (fileRUL!, [.removePreviousFile,.createIntermediateDirectories])
        }
  1. 僅僅指定文件目錄
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

下載完整鏈接請求(包含下載進度)

fileprivate func reqestDownLoad() -> Void {
        
        Alamofire.download(urlTitle, to: destination).downloadProgress { progress in
            print("Download Progress: \(progress.fractionCompleted)")
            }.validate().responseData {[weak self] response in
//            response.result.value:響應數據Data
            if response.destinationURL != nil {
                //self!.filePath = response.destinationURL:目的地址可直接使用
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章