How to properly download and save Big size file?

Hi,

I need to download and store Big size file on device. I did the following code:

WWW www = new WWW(url);
yield return www;
if (Succeeded(www))
SaveFile(www.bytes);:wink: //my method that save files

and it works everywhere apart iOS. It throws there Timeout and memoryException. I quess this happen, because I load file into the memory and after I save it on device. So I wroted to the following code:

WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(
(object sender, AsyncCompletedEventArgs e) =>
{
if (e.Error != null)
{
Debug.Log(e.Error.Message);
Debug.Log(e.Error.InnerException);
}
});
client.DownloadFileAsync(new Uri(url), path);

But I get the following exveption:
System.IO.IOException: The authentication or decryption has failed. —> Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
at Mono.Security.Protocol.Tls.RecordProtocol.ProcessAlert (AlertLevel alertLevel, AlertDescription alertDesc) [0x00000] in :0
at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in :0
— End of inner exception stack trace —
at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in :0
UnityEngine.Debug:Log(Object)
Assets.Scripts.Network.c__AnonStoreyD:<>m__26(Object, AsyncCompletedEventArgs) (at Assets/Scripts/Network/HttpService.cs:140)
System.Net.WebClient:m__E(Object)

Do You have any idea how to solve this issue?

I think you need this:

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public bool MyRemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        bool isOk = true;
        // If there are errors in the certificate chain, look at each error to determine the cause.
        if (sslPolicyErrors != SslPolicyErrors.None)
        {
            for (int i = 0; i < chain.ChainStatus.Length; i++)
            {
                if (chain.ChainStatus.Status != X509ChainStatusFlags.RevocationStatusUnknown)
                {
                    chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
                    chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
                    chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
                    chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
                    bool chainIsValid = chain.Build((X509Certificate2)certificate);
                    if (!chainIsValid)
                    {
                        isOk = false;
                    }
                }
            }
        }
        return isOk;
    }

and then before your download, set this:
csharp* *ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;* *

Here is a nice plugin, its very easy to use. You can download any small or big file from the web.
Must see this plugin Unity Asset Store - The Best Assets for Game Making

For more details visit this blog