在 iPhone 上继续中断的下载

发布于 2024-07-24 17:33:42 字数 187 浏览 2 评论 0原文

我使用 NSURLConnection 在 iPhone 上从网络下载大文件。 我使用“didReceiveData”方法将数据附加到“文档”文件夹中的文件中。 效果很好。

如果下载被中断(例如,因为用户按下“主页”按钮),我希望能够在用户下次启动我的应用程序时继续下载,而不是从头开始!

任何人都可以帮助我吗?

I use NSURLConnection to download large files from the web on iPhone. I use the "didReceiveData" method to append data to a file in the Documents folder. It works fine.

If the download is interrupted (for instance, because the user pressed the "home" button), I would like to be able to continue to download the next time the user launch my application, and not from scratch!

Anyone can help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-07-31 17:33:42

ASIHTTPRequest 具有易于使用的恢复下载支持:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#resume

或者,了解有多少数据您已经通过查看现有数据的大小进行了下载,并在 NSMutableURLRequest:

[request addValue:@"bytes=x-" forHTTPHeaderField:@"Range"];

..其中 x 是您已有数据的大小(以字节为单位)。 这将从字节 x 开始下载数据。 然后,您可以在收到文件时将此数据附加到您的文件中。

请注意,您要下载的 Web 服务器必须支持您尝试下载的资源的部分下载 - 如果是这样,它会发送 Accept-Ranges 标头。 您通常无法恢复动态生成的内容(例如服务器上的脚本生成的页面)的下载。

ASIHTTPRequest has easy to use support for resuming downloads:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#resume

Alternatively, find out how much data you have downloaded already by looking at the size of the existing data, and set the 'Range' header on your NSMutableURLRequest:

[request addValue:@"bytes=x-" forHTTPHeaderField:@"Range"];

..where x is the size in bytes of the data you have already. This will download data starting from byte x. You can then append this data to your file as you receive it.

Note that the web server you are downloading from must support partial downloads for the resource you are trying to download - it sends the Accept-Ranges header if so. You can't generally resume downloads for dynamically generated content (e.g. a page generated by a script on the server).

蹲在坟头点根烟 2024-07-31 17:33:42

我能想到的唯一方法是将您正在下载的文件分解成更小的文件。 您可以使用该

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

方法确定文件的每个部分何时完成,并在最后未下载的部分重新开始下载。

The only method I can think of is to break the file you're downloading up into smaller files. You can use the

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

method to determine when each portion of the file is finished, and restart the download at the last portion that didn't get downloaded.

罪#恶を代价 2024-07-31 17:33:42

查看 HTTP 标准,了解如何形成请求以从特定偏移量开始传输资源。 我暂时找不到任何代码,抱歉。

Look at the HTTP standard to see how you would form a request to start a transfer of a resource from a specific offset. I can't find any code offhand, sorry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文