- Home /
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()
{
}
}
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.
Your answer
Follow this Question
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