如何在iOS下载大文件?

时间:2011-06-12 14:56:08

标签: iphone ios

我正在尝试下载文件[> 40MB]来自Web服务器的HTTP请求。为此,我使用了apple提供的SimpleURLConnection示例。在该示例中,他们只下载图像文件,因此我修改了代码以下载pdf文件并将其存储在应用程序的文档目录中。这可以很好地下载小文件,但如果我尝试下载大文件[> 40Mb],它只下载6.4MB的数据。请帮我解决这个问题,

谢谢,

供参考:

使用下载数据编写文件的代码

  - (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);
}

2 个答案:

答案 0 :(得分:7)

不要重新发明轮子! ASIHTTP涵盖了您,see there。有一个特定的选项可以直接下载到文件。

答案 1 :(得分:2)

截至2011年9月,ASIHTTP已不再处于活跃开发状态。我发现RestKit在很多情况下都是一个不错的选择,但它缺少ASI的许多功能,例如下载暂停和恢复。

相关问题