【问题标题】:Move file failed after URLSession didFinishDownloadingToURLURLSession didFinishDownloadingToURL 后移动文件失败
【发布时间】:2015-08-29 05:29:31
【问题描述】:

我在模拟器上运行,我得到这个错误:

file:///Users/hunterp/Library/Developer/CoreSimulator/Devices/asd3243423-A1A4-4CFB-8A8F-234asdasd/data/Containers/Data/Application/4564561we-5FC5-4718-843B-sdasdwq4134/tmp/ CFNetworkDownload_o0xtzV.tmp 移动失败并出现错误:操作无法完成。 (可可 错误 4.)

我愿意:

    var test = DownloadClass(url: s, completionBlock: { (location) -> () in
        println (location)
        var toPath = self.getDocPath().stringByAppendingPathComponent("s.jpg")
        self.moveFile(location.absoluteString!, toThePath: toPath )
        println ("DONE")
    })
    test.start()

地点:

   func moveFile(fromPath: String, toThePath: String) {

        var error: NSError?
        let filemgr = NSFileManager.defaultManager()

        if filemgr.moveItemAtPath(fromPath, toPath: toThePath, error: &error) {
            println("Move successful")
        } else {
            println("Moved failed with error: \(error!.localizedDescription)")
        }
    }

    func getDocPath() -> String {
        return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
    }

class DownloadClass: NSObject,NSURLSessionDelegate,NSURLSessionDownloadDelegate {
    var session:NSURLSession?
    var downloadUrl:String
    var completion:(NSURL)->()
    init(url:String,completionBlock:(location:NSURL)->()){
        completion = completionBlock
        downloadUrl = url
        super.init()
        session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: nil)
    }
    func start(){
        let request = NSURLRequest(URL: NSURL(string: downloadUrl)!);
        let downloadTask = session!.downloadTaskWithRequest(request)
        downloadTask.resume()
    }
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
        completion(location)
    }
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        //here get progress
    }
}

【问题讨论】:

标签: ios xcode swift


【解决方案1】:

将移动路径代码更改为此,它将起作用

self.moveFile(location.path!, toThePath: toPath )

【讨论】:

  • 我没看到。你比我早 15 秒发表评论
  • @hunterp 你应该通过 downloadTask.response?.suggestedFilename 以正确的名称保存文件
猜你喜欢
  • 1970-01-01
  • 2022-01-18
  • 2016-09-14
  • 2013-12-17
  • 2017-12-06
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多