MonoTouch: How to save a huge PDF downloaded from

2019-08-11 22:50发布

问题:

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.