gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:01:53 25 4
gpt4 key购买 nike

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

我试着直接下载文件,它给出了正确的大小

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

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

附言。如果我更改为下载与 .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

最佳答案

方法 connectionDidFinishDownloading 似乎有问题。请使用下面的委托(delegate)方法,它正在工作

  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)")
}

关于iOS:下载 .TXT 文件给出奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com