如何在UserDirectory中获取url下载文件?

时间:2015-09-13 18:43:30

标签: swift alamofire

如何获取文件的网址?或者查看用户目录中的文件?

   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
    }
    `

1 个答案:

答案 0 :(得分:0)

您有目标目录 - .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),但是它很容易连接目录和路径并创建文件网址

let destinationPath = NSHomeDirectory().stringByAddingPathComponent(fileName)

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

let destinationURL = NSURL(filePath: destinationPath)!