- Home /
 
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 ?
Please format your code so people can read it. Use the "1001011" button after highlighting your code portion.
Answer by Samshosho · Jun 06, 2011 at 09:37 AM
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
Answer by bacalao · Feb 05, 2019 at 06:12 PM
 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;
     }
 
              Your answer
 
             Follow this Question
Related Questions
Save Unfinished Download 0 Answers
Download Links from GUI 2 Answers
Saving Local Data through WWW 1 Answer
Unity Blackberry - Save file on device 0 Answers