在 iPhone 上继续中断的下载
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASIHTTPRequest 具有易于使用的恢复下载支持:
http://allseeing-i.com/ASIHTTPRequest/How-to-use#resume
或者,了解有多少数据您已经通过查看现有数据的大小进行了下载,并在 NSMutableURLRequest:
..其中 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:
..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).
我能想到的唯一方法是将您正在下载的文件分解成更小的文件。 您可以使用该
方法确定文件的每个部分何时完成,并在最后未下载的部分重新开始下载。
The only method I can think of is to break the file you're downloading up into smaller files. You can use the
method to determine when each portion of the file is finished, and restart the download at the last portion that didn't get downloaded.
查看 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.