Alamofire.download() method: Where is the file and did it save successfully?

后端 未结 5 710
无人及你
无人及你 2021-02-02 11:16

The example for using Alamofire.download() works just fine, but there isn\'t any detail in how to access the resulting downloaded file. I can figure out where the file is and wh

5条回答
  •  攒了一身酷
    2021-02-02 11:42

    The example on the Alamofire readme file actually has the file path in it, so I grab it with a separate variable to use elsewhere in my code. It is not very elegant and I am hoping there is a way to get that info in the response, but for now, this is getting the job done:

    var fileName: String?
    var finalPath: NSURL?
    
    Alamofire.download(.GET, urlToCall, { (temporaryURL, response) in
    
        if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {    
    
            fileName = response.suggestedFilename!
            finalPath = directoryURL.URLByAppendingPathComponent(fileName!)
            return finalPath!
        }
    
        return temporaryURL
    })
        .response { (request, response, data, error) in
    
            if error != nil {
                println("REQUEST: \(request)")
                println("RESPONSE: \(response)")
            } 
    
            if finalPath != nil {
                doSomethingWithTheFile(finalPath!, fileName: fileName!)
            }
     }
    

提交回复
热议问题