Save a file from a URL to documents folder then use it doesn't work

Here is what i have in Unity
///////////////////////////////////////
var url = “http://domain/Movie/TestingMovie.mov”;
www = new WWW(url);
var progress1 = www;

print(progress1.progress);

while (!progress1.isDone)
yield;

var fileName : String = Application.persistentDataPath + “/” + “TestingMovie.mov”;
print(" progress1.bytes.length = "+progress1.bytes.length);

System.IO.File.WriteAllBytes(fileName, progress1.bytes);

Debug.Log("Cache saved: " + fileName);
print(“file download is done”);

yield WaitForEndOfFrame();

if (System.IO.File.Exists(“file://” + Application.persistentDataPath + “/TestingMovie.mov”))
{
print(" file does exist");
}
else
print(" file does not exist");

iPhoneUtils.PlayMovieURL((“file://” + Application.persistentDataPath + “TestingMovie.mov”), Color.black, iPhoneMovieControlMode.Full);

This is what i have output in Xcode
///////////////////////////////////////////////////
(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

progress1.bytes.length = 63708

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

Cache saved: /var/mobile/Applications/BAD0E1A0-FD53-4033-915B-D99C718173B3/Documents/TestingMovie.mov

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

file download is done

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

file does not exist

////////////////////////////////////////////////

Not sure what’s going on, even though the correct bytes.length is showing, then i ask it to write all bytes to the file name, but still the files does not exist, thus it does not play!

Something i am missing ?

here is the code nicely formated…

Here is what i have in Unity ///////////////////////////////////////

var url = "http://domain/Movie/TestingMovie.mov"; www = new 

        WWW(url); var progress1 = www;
        
        print(progress1.progress);
        
        while (!progress1.isDone) yield;
        
        var fileName : String = Application.persistentDataPath + "/" + "TestingMovie.mov"; 
    ``print(" progress1.bytes.length = "+progress1.bytes.length);
        
        System.IO.File.WriteAllBytes(fileName, progress1.bytes);
        
        Debug.Log("Cache saved: " + fileName); 
    ``print("file download is done");
        
        yield WaitForEndOfFrame();
        
        if (System.IO.File.Exists("file://" + Application.persistentDataPath + "/TestingMovie.mov")) { 
    ``print(" file does exist"); 
     ``} 
        else 
    ``print(" file does not exist");
        
        iPhoneUtils.PlayMovieURL(("file://" + Application.persistentDataPath + "TestingMovie.mov")
`    `, Color.black, iPhoneMovieControlMode.Full);

This is what i have output in Xcode /////////////////////////////////////////////////// (Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

progress1.bytes.length = 63708

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

Cache saved: /var/mobile/Applications/BAD0E1A0-FD53-4033-915B-D99C718173B3/Documents/TestingMovie.mov

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

file download is done

(Filename: /Applications/buildAgent/work/6bc5f79e0a4296d6/Projects/…/Runtime/Export/Generated/BaseClass.cpp Line: 2505)

file does not exist

////////////////////////////////////////////////

Not sure what’s going on, even though the correct bytes.length is showing, then i ask it to write all bytes to the file name, but still the files does not exist, thus it does not play!

Something i am missing

Hi,

the problem is the System.IO.File.Exists(“file://”) .

System.IO.File.Exists doesn’t like the protocol specification. I also learned it the hard way. This is not so well documented.
So the file exists but you always get a false in your if-clause.
just use (System.IO.File.Exists( Application.persistentDataPath + “/TestingMovie.mov”))
Or better make something like this:

//fileexists works only without file:// or http://	
Uri saveUri = new Uri(writePathWithFilename);

String savedUriPath = saveUri.LocalPath;
	
if(File.Exists(savedUriPath)){
.....
}

void Start()
{
StartCoroutine(savefile(“http://www.any.com/anything.jpg”, “myfile.jpg”, Application.dataPath));
}

    IEnumerator savefile(string myurl, string mytargetname, string folder)
    {
        string FileFullName = folder + "/" + mytargetname;
        if (System.IO.File.Exists(FileFullName))
        {
            print(mytargetname + " does exist");
            yield break;
        }
        else { print(mytargetname + " does not exist, downloading.."); }
 
        var www = new WWW(myurl);
 
        var progress1 = www;
 
        print(progress1.progress);
 
        while (!progress1.isDone) yield return null;
 
        print(" progress1.bytes.length = " + progress1.bytes.Length);
 
        System.IO.File.WriteAllBytes(FileFullName, progress1.bytes);
 
        Debug.Log("Cache saved: " + FileFullName);
        print("file download is done");
 
        yield break;
    }