MonoTouch: How to save a huge PDF downloaded from an URL incrementally?

别等时光非礼了梦想. 提交于 2019-12-24 08:39:11

问题


I need to download a huge PDF file from an URL in my MonoTouch iPhone/iPad newsstand app.

The PDF is too big to fit in memory, so I need to save it incrementally with NSData.

My idea is the following: instead of appending all the downloaded data in NSData and at the very end of the download, when I got all data, to store it to a file, I want to execute the download asynchronously in a separate thread and append to a file each received chunk and free my memory, but be certain that if the connection is dropped or the app crashes the download would resume automatically from the last chunk without corrupting the file.

What do you suggest? Is there a better method? How can I do it in MonoTouch? I did't find any documentation or code example about iOS incremental download/resume on the Xamarin website.


回答1:


You don't need NSData or anything from ObjC. You can use WebClient (http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) to achieve this in plain C#. For an example of a chunked download with progress bar, see here: http://devtoolshed.com/content/c-download-file-progress-bar




回答2:


You can use System.Net.WebClient and use the DownloadFile method, which will stream the data from HTTP to disk, without keeping it in memory.



来源:https://stackoverflow.com/questions/8666648/monotouch-how-to-save-a-huge-pdf-downloaded-from-an-url-incrementally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!