Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download File Using Alamofire 4.0 (Swift 3)

In older version of Alamofire. This is how I download file

    let destinationPath = Alamofire.Request.suggestedDownloadDestination( directory: .documentDirectory, domain: .userDomainMask);

    Alamofire.download(.GET, urlString, destination: destinationPath)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
//                print(totalBytesRead)
        }
        .response { request, response, _, error in

            let downloadedFilePath = destinationPath(URL(string: "")!, response!);

            NSUserDefaultsHelper.saveURL(downloadedFilePath, key: urlString);

            completion(downloadedFilePath, true);
    }

But now in the new version, my code is completely unusable and there is no similar function in the Alamofire library.

Any ideas please?

like image 419
JayVDiyk Avatar asked Oct 07 '16 08:10

JayVDiyk


3 Answers

I used to use this statements:

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

Alamofire.download(
    url,
    method: .get,
    parameters: parameters,
    encoding: JSONEncoding.default,
    headers: nil,
    to: destination).downloadProgress(closure: { (progress) in
        //progress closure
    }).response(completionHandler: { (DefaultDownloadResponse) in
        //here you able to access the DefaultDownloadResponse
        //result closure
    })

For more details read more in Alamofire docs about Migration to 4.0:

like image 142
pedrouan Avatar answered Oct 17 '22 23:10

pedrouan


Swift 4.0

 let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            documentsURL.appendPathComponent("file.csv")
            return (documentsURL, [.removePreviousFile])
        }

        Alamofire.download(url, to: destination).responseData { response in
            if let destinationUrl = response.destinationURL {
               print("destinationUrl \(destinationUrl.absoluteURL)")
            }
        }
like image 38
Mujahid Latif Avatar answered Oct 17 '22 23:10

Mujahid Latif


There are several enhancements in Alamofire 4. The first of which is the optionality of the destination closure. Now, by default, the destination closure is nil which means the file is not moved anywhere on the file system and the temporary URL is returned.

This is the default execution:-

Alamofire.download(urlString).responseData { response in
    print("Temporary URL: \(response.temporaryURL)")
}

This is my code to download file with Alamofire 4.0 which return destination Url of file:-

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        documentsURL.appendPathComponent("duck.png")
        return (documentsURL, [.removePreviousFile])
    }

    Alamofire.download(url, to: destination).responseData { response in
    if let destinationUrl = response.destinationURL ? {
        completionHandler(destinationUrl)
    }
}
like image 15
Shan Shafiq Avatar answered Oct 17 '22 23:10

Shan Shafiq