Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rad-Coders · May 30, 2016 at 04:53 AM · androidwwwsavingfile-iodownloading

Download and saving .txt from server to Android

Hi, I have run into a snag after trying to implement a retrieve and save function. I want to be able to download a .txt file from the server and then save it. But all the methods I have tried doesn't work, even just using the downloaded file directly (Which I seem to figure out as well if it were to be my second option).

Can anyone find wrong here that may give me a clue as to why it is not working. If it is possible can someone demonstrate how to use a file(the stream reader part because you need a filepath) once downloaded with the WWW class.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
    
 /*
  * newDat[0] - surname
  * newDat[1] - name
  * newDat[2] - title
  * newDat[3] - email
  * newDat[4] - birthday
  * newDat[5] - management
  * newDat[6] - subjects
  * newDat[7] - sport
  * newDat[8] - code
  * newDat[9] - guardian
  * newDat[10] - classroom
 */
 
 public class DataSorting : MonoBehaviour 
 {
     private string url = "https://www.dropbox.com/s/ijpxzvwo8o7es2a/StaffData.txt?dl=0";
     private string fileName = "StaffData.txt";
     private string filePath; 
 
     public static int fileLenght; //Lenght(Line count) of the file
 
     private string[] unsortedData;
 
     public static string[] newDat = new string[12];
 
     private StreamReader dataReader;
 
     public static List<string>[] teacherData; //List that stores all data on the teachers
 
     void ReadData()
     {
         filePath = Application.persistentDataPath + "/" + fileName;
         dataReader = new StreamReader(filePath);
         unsortedData = new string[fileLenght];
         for(int i = 0; i < fileLenght; i++)
         {
             unsortedData[i] = dataReader.ReadLine();
         }
         dataReader.Close();
         teacherData = new List<string>[fileLenght];
     }
 
     void SortData()
     {
         for(int i = 0; i < fileLenght; i++)
         {
             newDat = unsortedData[i].Split(',');
             teacherData[i] = new List<string>();
             teacherData[i].Add(newDat[0]);
             teacherData[i].Add(newDat[1]);
             teacherData[i].Add(newDat[2]);
             teacherData[i].Add(newDat[3]);
             teacherData[i].Add(newDat[4]);
             teacherData[i].Add(newDat[5]);
             teacherData[i].Add(newDat[6]);
             teacherData[i].Add(newDat[7]);
             teacherData[i].Add(newDat[8]);
             teacherData[i].Add(newDat[9]);
             teacherData[i].Add(newDat[10]);
         }
         print (teacherData[0][2]);
     }
 
     IEnumerator DownloadData()
     {
         WWW www = new WWW(url);
         yield return www;
 
         File.WriteAllText(Application.persistentDataPath + "/" + fileName, www.text);
         print ("doen");
     }
 
     void Awake() 
     {
         StartCoroutine(DownloadData());
 
         fileLenght = File.ReadAllLines(filePath).Length;
 
         ReadData();
         SortData();
     }
     
 
     void Update() 
     {
     
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · May 30, 2016 at 07:01 AM

Your logic would be to first check if the file exists on the device, and if not then you download.

 string file = null;
 void RetrieveData(){
      if(File.Exists(path)){
             this.file = File.ReadAllText(path);
             return;
      }
      StartCoroutine(DownloadData());
 }
 
 IEnumerator DownloadData(){
        WWW www = new WWW(url);
        yield return www;
        // Check www is right
        this.file = www.text;
        File.WriteAllText(path, this.file);
 }

Just to make it easier, instead of using your own file system, I would consider json or xml. You can then use parsers to read, write data.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

downloading file, saving on android 1 Answer

About www.error! 1 Answer

Sending to API special french characters 0 Answers

Unity download audio 1 Answer

PlayerPrefs not saving 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges