如何在iOS下载大文件? [英] How to download large files in iOS?

查看:189
本文介绍了如何在iOS下载大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to download files [> 40MB] from web server over HTTP request. To do that I've used the SimpleURLConnection sample provided by the apple. In that sample they only download the image files, so I modified the code to download pdf files and stored it in application's document directory. This is working fine to download small files, but it only download 6.4MB of data if I trying to download large files [>40Mb]. please help me to fix this,

谢谢,

FYI:

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

code to write file with downloaded data

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


推荐答案

不要重新发明轮子!
ASIHTTP为您提供服务,查看。有一个特定的选项可以直接下载到文件。

Don't reinvent the wheel! ASIHTTP has you covered, see there. There's a specific option to download directly to file.

这篇关于如何在iOS下载大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆