在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的DownloadDestination
Posted
技术标签:
【中文标题】在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的DownloadDestination【英文标题】:Setting Alamofire custom destination file name instead of using suggestedDownloadDestination in Swift 3.0 【发布时间】:2016-12-14 06:57:52 【问题描述】:如何在 swift 3.0 中编写以下 sn-p ?以下语法在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)
【问题讨论】:
获取响应 mime 类型并添加正确的文件扩展名 Alamofire 版本是最新的 4.2.0 和 swift 3.0 【参考方案1】:在 Swift 3 中是这样的。
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 Documentation
或Alamofire 4.0 Migration Guide
了解更多详情。
【讨论】:
在我上面的代码中,它的 let pathComponent = response.suggestedFilename。这个怎么写? @ZubinGala 我没试过,但是,改变这样的东西,在DownloadRequest.DownloadFileDestination
用响应替换第二个_
,然后使用let pathComponent = response.suggestedFilename
@ZubinGala 欢迎朋友 :)【参考方案2】:
使用func appendingPathComponent(_ pathComponent: String) -> URL
代替appendPathComponent
。
let destination: DownloadRequest.DownloadFileDestination = _, _ in
let pathComponent = "yourfileName"
let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
也可以使用response
。
let destination: DownloadRequest.DownloadFileDestination = _, response in
let pathComponent = response.suggestedFilename!
let directoryURL: URL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
【讨论】:
以上是关于在 Swift 3.0 中设置 Alamofire 自定义目标文件名而不是使用建议的DownloadDestination的主要内容,如果未能解决你的问题,请参考以下文章
如何在 DataProvider 类 Alamofire 函数 swift 中设置协议函数不能正确调用
在 Swift 3.0 (Alamofire 4.4.0) 中的一些请求后,Alamofire 停止工作