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

后端 未结 5 830
无人及你
无人及你 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条回答
  •  萌比男神i
    2021-02-02 11:27

    Here is complete method to download file in different destination with progress

    //MARK: Download methods

    func downloadFile(reqType : RequestType,  urlParam: String,completionHandler: (Double?, NSError?) -> Void) {
    
        let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
        var downloadPath = documentsPath.URLByAppendingPathComponent("downloads")
        var isDirectory: ObjCBool = false
        if NSFileManager.defaultManager().fileExistsAtPath(downloadPath.path!, isDirectory: &isDirectory) {
            if(!isDirectory){
                do {
                    try NSFileManager.defaultManager().createDirectoryAtPath(downloadPath.path!, withIntermediateDirectories: true, attributes: nil)
                }catch  {
                    NSLog("Unable to create directory ")
                }
            }
        }else{
            do {
                try NSFileManager.defaultManager().createDirectoryAtPath(downloadPath.path!, withIntermediateDirectories: true, attributes: nil)
            }catch  {
                NSLog("Unable to create directory ")
            }
    
        }
    
    
        //get the url from GTM
        let urlString = self.getRequestedUrlFromIdentifier(reqType, param: urlParam)
        let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
        Alamofire.download(.GET, urlString, destination: { (temporaryURL, response) in
    
            let pathComponent = response.suggestedFilename
            downloadPath = downloadPath.URLByAppendingPathComponent(pathComponent!)
            if NSFileManager.defaultManager().fileExistsAtPath(downloadPath.path!) {
                do{
                 try NSFileManager.defaultManager().removeItemAtPath(downloadPath.path!)
                }catch {
    
                }
            }
            return downloadPath
        })            .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
    
                // This closure is NOT called on the main queue for performance
                // reasons. To update your ui, dispatch to the main queue.
                dispatch_async(dispatch_get_main_queue()) {
                    print("Total bytes read on main queue: \(totalBytesRead)")
                }
            }
            .response { request, response, _, error in
                print(response)
                let originalPath = destination(NSURL(string: "")!, response!)
                if let error = error {
                    completionHandler(500000.1 , nil)
                    print("Failed with error: \(error)")
                } else {
                    completionHandler(500000.0 , nil)
                    print("Downloaded file successfully \(downloadPath)")
                }
        }
    
    }
    

提交回复
热议问题