在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的下载目的地 [英] Setting Alamofire custom destination file name instead of using suggestedDownloadDestination in Swift 3.0
本文介绍了在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的下载目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 swift 3.0 中编写以下代码片段?以下语法在 swift 2
How to write the following snippet in swift 3.0 ? The following syntax is in swift 2
Alamofire.download(.POST, invoice.url,parameters:params, destination: { (url, response) -> NSURL in
let pathComponent = response.suggestedFilename
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileUrl = directoryURL.URLByAppendingPathComponent(pathComponent)
return fileUrl
})
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
dispatch_async(dispatch_get_main_queue()) {
let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead)
completionHandler(progress, nil)
}
}
.responseString { response in
print(response.result.error)
completionHandler(nil, response.result.error)
}
推荐答案
在 Swift 3 中是这样的.
In Swift 3 it is something like this.
let parameters: Parameters = ["foo": "bar"]
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let pathComponent = "yourfileName"
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendPathComponent(pathComponent)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
print("Progress: (progress.fractionCompleted)")
}
.validate { request, response, temporaryURL, destinationURL in
// Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
print(response.temporaryURL)
print(response.destinationURL)
}
检查 Alamofire 文档
或 Alamofire 4.0 迁移指南
了解更多详情.
Check Alamofire Documentation
or Alamofire 4.0 Migration Guide
for more details.
这篇关于在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的下载目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文