Alamofire不下载PDF

时间:2017-03-15 18:03:44

标签: swift swift3 alamofire

我正在尝试将PDF下载到文档目录。请求成功,但请求完成后文件目录中不存在该文件。

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

let documentsDirectory = FileManager.SearchPathDirectory.documentDirectory
let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(documentsDirectory, userDomainMask, true)

if paths.first != nil 
{

    Alamofire.download("http://www.test.com/pdfs/sample.pdf", to: destination).responseData 
    { response in
        if response.result.isFailure
        {
            print("\(response.error!)")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

以下内容适用于我:

let fileExtension: String = fileUrl!.pathExtension
let documentURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let filePath = documentURL.appendingPathComponent("\(disk_file_name!).\(fileExtension)")

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
     return (filePath, [.removePreviousFile])
}

Alamofire.download(URL(string: fileUrl)!, to: destination) ...