[Answer]-Use AFNetworking to download a file from Django server

1👍

Yes that is correct.

Take a look at the doc here – http://cocoadocs.org/docsets/AFNetworking/2.0.0/Classes/AFHTTPRequestOperationManager.html#//api/name/responseSerializer. You need to specify a response serializer for AFHttpRequestOperationManager.

It seems like you are downloading a plain text file, so you can use the default serializer. Add this call:

self.man.responseSerializer = AFHttpResponseSerializer()

before you call self.man.GET.

The https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide has some details on how the response and request serializers work.

👤tng

0👍

The code below is the correct way to do it for AFNetwokring 2.5.0

Reference: http://cocoadocs.org/docsets/AFNetworking/2.5.0/

        var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        var man = AFURLSessionManager(sessionConfiguration: configuration)

        var URL = NSURL(string: "http://www.google.com/images/srpr/logo11w.png")
        var request = NSURLRequest(URL:URL!)

        var downloadTask = man.downloadTaskWithRequest(request, progress: nil,
        destination:{(targetPath:NSURL!,response:NSURLResponse!) -> NSURL! in
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        let outPath = String(format: "%@/download/", documentsPath)
        var url:NSURL! = NSURL(fileURLWithPath: outPath)
        return url.URLByAppendingPathComponent(response.suggestedFilename as String!)
        },
        completionHandler:{(response:NSURLResponse!,filePath:NSURL!,error:NSError!)  in
            println(response.suggestedFilename)
        })

        downloadTask.resume();
👤Zidong

Leave a comment