【发布时间】:2015-10-22 13:03:18
【问题描述】:
我有下一个从 url 下载文件的代码。但我收到下一个错误:
失败并出现错误:错误域=NSCocoaErrorDomain 代码=516 ““CFNetworkDownload_85Dvq4.tmp”无法移动到“文档” 因为已经存在同名项目。” UserInfo={NSSourceFilePathErrorKey=/Users/otani/Library/Developer/CoreSimulator/Devices/8BABC203-7FD9-4F03-BF0D-D660DD29CCDE/data/Containers/Data/Application/09C714D3-F185-4289-91DB-13B21557B784/tmp/CFNetworkDownload_85Dvq4。温度, NSUserStringVariant=( 移动),NSDestinationFilePath=/Users/otani/Library/Developer/CoreSimulator/Devices/8BABC203-7FD9-4F03-BF0D-D660DD29CCDE/data/Containers/Data/Application/09C714D3-F185-4289-91DB-13B21557B784/Documents/temp.法尔格, NSFilePath=/Users/otani/Library/Developer/CoreSimulator/Devices/8BABC203-7FD9-4F03-BF0D-D660DD29CCDE/data/Containers/Data/Application/09C714D3-F185-4289-91DB-13B21557B784/tmp/CFNetworkDownload_85Dvq4.tmp, NSUnderlyingError=0x7fea5a53a9a0 {错误域=NSPOSIXErrorDomain Code=17 "文件存在"}}
下载文件代码如下:
func downloadFile(let url: String!){
self.downloadHUD()
Alamofire.download(.GET, url) { temporaryURL, response in
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let fullPath = documentsUrl.URLByAppendingPathComponent(Misc.pathToSaveFile)
if (NSFileManager.defaultManager().fileExistsAtPath(fullPath.absoluteString)){
try! NSFileManager.defaultManager().removeItemAtURL(fullPath)
}
return fullPath
}
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
dispatch_async(dispatch_get_main_queue()) {
self.hud.progress = Float(totalBytesRead) / Float(totalBytesExpectedToRead)
print("Total bytes read on main queue: \(totalBytesRead)")
}
}
.response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
self.hideHUD()
}
}
}
错误上线
print("Failed with error: \(error)")
【问题讨论】: