iOS: 下载 .TXT 文件给出奇怪的结果
iOS: Download .TXT file give strange result
我正在尝试在一个网站上下载 5MB 大小的 .txt 文件
我尝试直接下载文件,它给出了正确的大小
我尝试通过 Android DownloadManager.Request 下载,它也给出了正确的大小
Except, 在 iOS 中,当我尝试通过 NSURLConnection 下载它时,
没有 1 秒,它显示成功下载 5MB 大小
但是,当我检查网络时,它显示我只使用了 25kb?
PS。如果我更改为下载与 .zip 文件相同的域,它会正确下载
问题link:http://www.bluewindsolution.com/speed_test/downloads/download_file.txt
工作link:
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: \()")
}
输出结果
模拟器中的网络接收结果
我推荐你使用库 Alamofire:https://github.com/Alamofire/Alamofire
这是一个非常简单易用的简单 HTTP 网络库。
您可以这样下载文件:
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
方法 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)")
}
我找到了一个有趣的答案
我的download_file.txt只有字符'x'大约500万个字符变成5MB
iOS 有 'some' 算法检查下载文件是否有相同的字符或文本(它也不是缓存)并且不需要下载整个 5MB,而是使用网络包只有 25KB,然后自己生成那个重复字符?
根据我的测试,我插入了另一个包含混合字符的文件名 download_file_ios.txt,它下载了完整的 5MB
我希望有一天能发布这个算法
PS。感谢@Inder Kumar Rathore 指出我检查下载文件大小
Network Usage only 25 KB
But I get file with 5MB size
我正在尝试在一个网站上下载 5MB 大小的 .txt 文件
我尝试直接下载文件,它给出了正确的大小
我尝试通过 Android DownloadManager.Request 下载,它也给出了正确的大小
Except, 在 iOS 中,当我尝试通过 NSURLConnection 下载它时, 没有 1 秒,它显示成功下载 5MB 大小 但是,当我检查网络时,它显示我只使用了 25kb?
PS。如果我更改为下载与 .zip 文件相同的域,它会正确下载
问题link:http://www.bluewindsolution.com/speed_test/downloads/download_file.txt
工作link: 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: \()")
}
输出结果
模拟器中的网络接收结果
我推荐你使用库 Alamofire:https://github.com/Alamofire/Alamofire
这是一个非常简单易用的简单 HTTP 网络库。
您可以这样下载文件:
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
方法 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)")
}
我找到了一个有趣的答案
我的download_file.txt只有字符'x'大约500万个字符变成5MB
iOS 有 'some' 算法检查下载文件是否有相同的字符或文本(它也不是缓存)并且不需要下载整个 5MB,而是使用网络包只有 25KB,然后自己生成那个重复字符?
根据我的测试,我插入了另一个包含混合字符的文件名 download_file_ios.txt,它下载了完整的 5MB
我希望有一天能发布这个算法
PS。感谢@Inder Kumar Rathore 指出我检查下载文件大小
Network Usage only 25 KB
But I get file with 5MB size