我试图通过HTTP请求从Web服务器上下载文件[> 40MB]。为此,我使用了Apple提供的SimpleURLConnection示例。在这个示例中,他们只下载图像文件,所以我修改了代码以下载pdf文件并将其存储在应用程序的文档目录中。这可以很好地下载小文件,但如果我尝试下载大文件[> 40Mb],它只会下载6.4MB的数据。请大家帮我解决这个问题,如何在iOS下载大文件?
谢谢
FYI:
代码写入文件与下载的数据
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
// A delegate method called by the NSURLConnection as data arrives. We just
// write the data to the file.
{
#pragma unused(theConnection)
NSInteger dataLength;
const uint8_t * dataBytes;
NSInteger bytesWritten;
NSInteger bytesWrittenSoFar;
assert(theConnection == self.connection);
dataLength = [data length];
dataBytes = [data bytes];
bytesWrittenSoFar = 0;
do {
bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self _stopReceiveWithStatus:@"File write error"];
break;
} else {
bytesWrittenSoFar += bytesWritten;
}
} while (bytesWrittenSoFar != dataLength);
}
你有没有调试信息?哦,'assert(theConnection == self.connection)'是如此可怕,以至于如果你使用多个连接,这段代码注定会崩溃。使用'if(theConnection!= self.connection){return;而不是。 – 2011-06-12 14:59:14