swift - 如何获取用户目录中的下载文件的url?

标签 swift alamofire

如何获取文件的url?或者查看用户目录中有哪些文件?

   let destination = Alamofire.Request.suggestedDownloadDestination(directory: .UserDirectory, domain: .UserDomainMask)
    Alamofire.download(.GET, "http://best-muzon.com/dl/NNE2IxI6LYNtocG2fmFn3Q/1442201485/songs12/2015/02/vremja-i-steklo-imja-505-(best-muzon.com).mp3", destination: destination)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in

            dispatch_async(dispatch_get_main_queue()) {
                //print("Total bytes read on main queue: \(totalBytesRead)")
                self.label.text = String(totalBytesRead)
            }
        }
        .response { request, response, _, error in
    }
    `

最佳答案

您拥有目标目录 - .UserDirectory 参数 - 和文件名 - URL 的最后一个路径部分

let url = "http://best-muzon.com/dl/NNE2IxI6LYNtocG2fmFn3Q/1442201485/songs12/2015/02/vremja-i-steklo-imja-505-(best-muzon.com).mp3"
let fileName = url.lastPathComponent

我不知道 .UserDirectory 是主文件夹 (~) 还是下载文件夹 (~/Downloads),但连接目录和路径并创建文件 URL 很容易

let destinationPath = NSHomeDirectory().stringByAddingPathComponent(fileName)

let destinationPath = NSHomeDirectory().stringByAddingPathComponent("Downloads/\(fileName)")

let destinationURL = NSURL(filePath: destinationPath)!

关于swift - 如何获取用户目录中的下载文件的url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553259/

相关文章:

iphone - 多部分图像的 ALAMOFIRE 响应图像数据

ios - 在 Alamofire POST 请求中发送一个数组作为参数

macos - 为什么窗口背景在全屏时保持黑色?

swift - 在 Playground 结合

swift 2.2 : Type '()' does not conform to protocol 'BooleanType'

ios - 当键盘出现在 SwiftUI 中时向上移动 TextField

ios - UIWebView 无需转到 iOS 的视频播放器即可播放 Html 5 视频

ios - 如何为 Alamofire POST 请求正文设置 UTF8 编码?

swift - 如何将模型的 (json) 值发送到 View Controller ?

ios - 获取 POST 内容类型错误