iOS:下载.TXT文件给出奇怪的结果

时间:2015-05-15 14:20:36

标签: ios swift nsurlconnection nsurl

我正在尝试在一个网站上下载大小为5MB的.txt文件

我尝试直接下载文件并提供正确的大小

我尝试通过Android DownloadManager.Request下载,它也提供正确的大小

除了,在iOS中,当我尝试通过NSURLConnection下载时, 没有1秒,它显示5MB大小的成功下载 但是,当我检查网络接收它显示我只使用了25kb?

PS。如果我更改为使用.zip文件下载相同的域名,则会正确下载

问题链接:http://www.bluewindsolution.com/speed_test/downloads/download_file.txt

工作环节: http://www.bluewindsolution.com/speed_test/downloads/download_file.zip

这是代码

@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: \()")
}

输出结果

enter image description here

模拟器中的网络接收结果

enter image description here

3 个答案:

答案 0 :(得分:1)

似乎方法connectionDidFinishDownloading的问题。 请使用以下委托方法,它正在运作

  var mData = NSMutableData()
  func connection(connection: NSURLConnection, didReceiveData data: NSData) {
    mData.appendData(data)
    println("loading")
  }

  func connectionDidFinishLoading(connection: NSURLConnection) {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
    let filePath = documentsPath.stringByAppendingPathComponent("text.txt")
    mData.writeToFile(filePath, atomically: true)
    println("\(filePath)")
  }

  func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    println("\(error)")
  }

答案 1 :(得分:0)

我建议你使用图书馆Alamofire:https://github.com/Alamofire/Alamofire

这是一个非常简单易用的简单HTTP网络库。

你可以像这样下载文件juste:

    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)

答案 2 :(得分:0)

我找到了一个有趣的答案

我的download_file.txt只有字符'x',大约有500万个字符变成5MB

iOS有'某些'算法,检查下载文件是否具有相同的字符或文本(它也不是缓存)并且不需要下载整个5MB,而是使用仅25KB的网络包,然后生成该重复字符本身?

从我的测试中,我插入另一个文件名download_file_ios.txt,其中包含混合字符,并以5MB的速度下载

我希望有一天会发布这个算法

PS。感谢@Inder Kumar Rathore指出我检查下载文件大小

Network Usage only 25 KB

  

网络使用量仅为25 KB

enter image description here

  

但我得到5MB大小的文件