我正在使用NSURLDownload在Mac临时文件夹中下载zip文件.这是代码: func function () { var request:NSURLRequest = NSURLRequest(URL: NSURL(string: self.downloadLink.stringValue)!) var download:NSURLDownload = NSURLDownload(re
func function () {
var request:NSURLRequest = NSURLRequest(URL: NSURL(string: self.downloadLink.stringValue)!)
var download:NSURLDownload = NSURLDownload(request: request, delegate: self)
}
func download(download: NSURLDownload, decideDestinationWithSuggestedFilename filename: String) {
tempPath = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString)
download.setDestination(tempPath.stringByAppendingPathExtension("zip")!, allowOverwrite: false)
}
这有效,但我试图通过附加路径组件将我的zip下载隔离到我刚刚创建的临时文件夹中:
tempPath = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString).stringByAppendingPathComponent("thisShouldBeTheNameOfTheFile")
在这种情况下,下载不起作用,不创建任何内容并且不调用函数downloadDidFinish.
临时目录是否受到保护,因此我无法在其中创建新文件夹?我怎样才能解决这个问题?
download.setDestination方法,如果目录不存在,则不会自动创建目录.试试这个:
func download(download: NSURLDownload, decideDestinationWithSuggestedFilename filename: String) {
let tempPathDirectory = NSTemporaryDirectory().stringByAppendingPathComponent(NSProcessInfo().globallyUniqueString)
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(tempPathDirectory) == false {
fileManager.createDirectoryAtPath(tempPathDirectory, withIntermediateDirectories: true, attributes: nil, error: nil)
}
let tempPath = tempPathDirectory.stringByAppendingPathComponent("thisShouldBeTheNameOfTheFile")
download.setDestination(tempPath.stringByAppendingPathExtension("zip")!, allowOverwrite: false)
}
希望这对你有所帮助!
