我正在尝试将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!)")
}
}
}
答案 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) ...