Skip to content

Instantly share code, notes, and snippets.

@blue0513
Last active January 16, 2018 05:24
Show Gist options
  • Save blue0513/ba2f6fd7e956edc0f51f10e449a00d5e to your computer and use it in GitHub Desktop.
Save blue0513/ba2f6fd7e956edc0f51f10e449a00d5e to your computer and use it in GitHub Desktop.
Post local csv data to remote server
using UnityEngine;
using System.Collections;
using System.IO;
// use it with uploader php
// https://gist.github.com/blue0513/7f772c0ad1e78903a3839e78bffbba0a
public class PostDataToServer : MonoBehaviour {
private string folderPath = "/Data";
private string phpUrl = "http://foo/bar/upload.php"
void Awake(){
createDir();
}
void Start () {
if(NetConnection()){
StartCoroutine("postData");
}
}
private IEnumerator postData(){
string base_filepath;
#if UNITY_EDITOR
base_filepath = Application.dataPath;
#else
base_filepath = Application.persistentDataPath;
#endif
// execute all the csv files
foreach (string stFilePath in System.IO.Directory.GetFiles(base_filepath + folderPath + "/", "*.csv")) {
string filename = System.IO.Path.GetFileName(stFilePath);
string filepath;
#if UNITY_EDITOR
filepath = "file:///" + Application.dataPath + folderPath + "/" + filename;
#else
filepath = "file:///" + Application.persistentDataPath + folderPath + "/" + filename;
#endif
WWWForm wwwForm = new WWWForm ();
WWW www = new WWW (filepath);
yield return www;
if (www.error != null){
Debug.Log("ERROR in Upload");
Debug.LogError (www.error);
}else{
byte[] data = System.Text.Encoding.Unicode.GetBytes(www.text);
wwwForm.AddBinaryData("theFile", data, filename , "text/csv");
wwwForm.AddField("name" , filename);
WWW testAccess;
testAccess = new WWW(phpUrl , wwwForm);
yield return testAccess;
// if php returns "success", upload succeeded
if(testAccess.text != "success"){
// Nothing to do
}else if(testAccess.text == "success"){
// delete local file
#if UNITY_EDITOR
string deletefilepath = Application.dataPath + folderPath + "/" + filename;
#else
string deletefilepath = Application.persistentDataPath + folderPath + "/" + filename;
#endif
FileDelete(deletefilepath);
}
yield return null;
}
}
}
private void createDir() {
#if UNITY_EDITOR
if(!Directory.Exists(Application.dataPath + folderPath)){
Directory.CreateDirectory(Application.dataPath + folderPath);
}
#else
if(!Directory.Exists(Application.persistentDataPath + folderPath)){
Directory.CreateDirectory(Application.persistentDataPath + folderPath);
}
#endif
}
private void FileDelete(string targetFilePath){
if (!File.Exists (targetFilePath)) {
return;
}
File.SetAttributes(targetFilePath, FileAttributes.Normal);
File.Delete(targetFilePath);
}
private bool NetConnection(){
switch ( Application.internetReachability ) {
//ネットワークに接続不可な場合の処理
case NetworkReachability.NotReachable:
Debug.Log("network not connected");
return false;
//break;
//ネットワークに接続可能な場合の処理
default:
Debug.Log("network connected");
return true;
//break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment