Search code examples
iosswiftnsurlconnectionnsurl

iOS: Download .TXT file give strange result


I'm trying to download .txt file that has 5MB size in one website

I tried to download file directly and it give the correct size

I tried to download via Android DownloadManager.Request and it also give the correct size

Except, in iOS, while I tried to download it via NSURLConnection, without a 1 second, it show successful download with 5MB size however, when I check in Network receiving it show I just used only 25kb?

PS. If I change to download the same domain with .zip file, it download properly

problem link: http://www.bluewindsolution.com/speed_test/downloads/download_file.txt

working link: http://www.bluewindsolution.com/speed_test/downloads/download_file.zip

here is the code

@IBAction func buttonClick(sender: AnyObject) {
    let timestamp = NSString(format: "%.0f", NSDate().timeIntervalSince1970)
    let url:NSURL = NSURL(string: "http://www.bluewindsolution.com/speed_test/downloads/download_file.txt?time=\(timestamp)")!
    var request:NSURLRequest = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 30.0)
    startDate = NSDate()
    NSURLConnection(request: request, delegate: self)
}

func connectionDidFinishDownloading(connection: NSURLConnection, destinationURL: NSURL) {
    println("finishdownload \(destinationURL)")

    println("timetotal: \(timetotal)")
    println("speed: \(speed) mbps")
    println("filesize: \(filesize)") // it's show 5MB but network just receive only 25KBps ?  it's also not the cache because it's happen at the first time also.

    connection.cancel()
}

func connection(connection: NSURLConnection, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, expectedTotalBytes: Int64) {
    let currentDate:NSDate = NSDate()

    let tt = currentDate.timeIntervalSinceDate(startDate)
    timetotal = tt // to get total time
    speed = Double(totalBytesWritten)/1024/1024*8/tt  // to get speed MBps
    filesize = Double(totalBytesWritten)/1024/1024  // to get as MB

    // use this to get result as real time
    //println("timetotal: \(totaltime)")
    //println("speed: \() mbps")
    //println("filesize: \()")
}

Output result

enter image description here

Result of Network Receive in Emulator

enter image description here


Solution

  • I found an interesting answer

    my download_file.txt has only character 'x' about 5 million characters to become 5MB

    iOS has 'some' algorithm that check the download file has the same character or text (it's also not the cache) and it no need to download the whole 5MB, instead it use network package only 25KB and then generate that repeat character itself ?

    from my test, I insert another file name download_file_ios.txt that has mixed characters, and it download with full 5MB

    I hope someday this algorithm will be posted

    PS. Thanks @Inder Kumar Rathore for pointing me out to check the download file size

    Network Usage only 25 KB

    Network Usage only 25 KB

    enter image description here

    But I get file with 5MB size