How to check if a file is available in a path,if not download from server?

Hi, I check for a file in a path.If the file is not present I wanto download from the server.The code I tried is given below .Even though the file is present in the path it again downloads it?What is the problem?

IEnumerator DownloadWorldmap()
    {

        //string datapath1 = "data.txt";
        //string datapath2 = "world.txt";
        //StartCoroutine(CheckTextFile(datapath1));
        //StartCoroutine(CheckTextFile(datapath2));


        for (int i = 0; i < Allfilenames.Count;i++)
        {
            Debug.Log("File name === "+Allfilenames[i]);
            WWW localFile = new WWW(Application.persistentDataPath+"/"+Allfilenames[i]);
            yield return localFile;

            if (localFile.error == null)
            {
                Debug.Log("File Already There");

            }

            else
            {


                var uwr = new UnityWebRequest("https://test.com/casino/hariupload/uploads/" + Allfilenames[i], UnityWebRequest.kHttpVerbGET);
                string path = Path.Combine(Application.persistentDataPath, Allfilenames[i]);
                uwr.downloadHandler = new DownloadHandlerFile(path);
                yield return uwr.SendWebRequest();
                if (uwr.isNetworkError || uwr.isHttpError)
                    Debug.LogError(uwr.error);
                else
                    Debug.Log("File successfully downloaded and saved to " + path);
            
            }


        }


   }

I am getting this message in the Log “File successfully downloaded and…” 4 times as there are 4 files present in the server.

WWW is horribly inefficient way to check whether the file exists and you should also pass URI to it, not path (it should be file:///…).

Why you are not using File.Exists() ?

2 Likes