thieumao

Upload - Download File with Parse in Unity

Sep 18th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Parse;
  7.  
  8. public class ParseBigFile : MonoBehaviour {
  9.  
  10.     //String linkdownload = "http://files.parsetfss.com/8d7d8fb2-b0bc-4d95-9463-7b2397d0e7ae/tfss-d1e15eac-8c8a-43fd-845b-5f0cf4051b8a-LuongDuLieu.txt";
  11.     String linkdownload = "";
  12.  
  13.     // Use this for initialization
  14.     void Awake()
  15.     {
  16.         print("Running..");
  17.     }
  18.  
  19.     void Start() {
  20.         // upload file with parse
  21.         /*
  22.         int soto = 1;
  23.         for (int i = 0; i < soto; i++) {
  24.             //byte[] data = System.Text.Encoding.UTF8.GetBytes("File DataUser Client " + i);
  25.             //ParseFile file = new ParseFile("resume.txt", data);
  26.            
  27.             String nameFile = "LuongDuLieu.txt";
  28.             String filename = Application.persistentDataPath + "/" + nameFile;
  29.             byte[] bytes = System.IO.File.ReadAllBytes(filename);
  30.             ParseFile file = new ParseFile(nameFile, bytes);
  31.  
  32.             ParseObject gameScore = new ParseObject("TestBigData");
  33.             gameScore["file"] = file;
  34.             gameScore["id_facebook"] = "idfb " + i;
  35.             gameScore["score"] = i;
  36.             gameScore["level"] = i;
  37.  
  38.             gameScore.SaveAsync();
  39.  
  40.             print("i= " + i + " time = " + Time.deltaTime);
  41.  
  42.  
  43.         }
  44.         */
  45.  
  46.         // write file
  47.         /*
  48.         ParseQuery<ParseObject> query = ParseObject.GetQuery("TestBigData");
  49.         query.GetAsync("A04WgJ976D").ContinueWith(t =>
  50.         {
  51.             ParseObject obj = t.Result;
  52.  
  53.             int level = obj.Get<int>("level");
  54.             string id_facebook = obj.Get<string>("id_facebook");
  55.             ParseFile file = obj.Get<ParseFile>("file");
  56.             print("level = " + level);
  57.             print("id_facebook = " + id_facebook);
  58.             print("file = " + file.Url.AbsoluteUri);
  59.  
  60.             linkdownload = file.Url.AbsoluteUri;
  61.  
  62.  
  63.  
  64.         });
  65.  
  66.      
  67.         String linkUrl = "http://files.parsetfss.com/8d7d8fb2-b0bc-4d95-9463-7b2397d0e7ae/tfss-d1e15eac-8c8a-43fd-845b-5f0cf4051b8a-LuongDuLieu.txt";
  68.         WWW www = new WWW(linkUrl);
  69.         StartCoroutine(Download(www));
  70.         */
  71.  
  72.         // download file with parse
  73.         StartCoroutine(LoadFile());
  74.     }
  75.  
  76.  
  77.     IEnumerator Download(WWW www)
  78.     {
  79.         yield return www;
  80.         print("du lieu = " + www.text);
  81.         byte[] bytes = null;
  82.         bytes = www.bytes;
  83.  
  84.         String filename = Application.persistentDataPath + "/" + "LuongDuLieu.txt";
  85.  
  86.         System.IO.File.WriteAllBytes(filename, bytes);
  87.  
  88.     }
  89.  
  90.     //void Update() { StartCoroutine(LoadFile()); }
  91.  
  92.     IEnumerator LoadFile()   //single method downloads file
  93.     {
  94.         Debug.Log("LoadFile()");
  95.  
  96.         ParseQuery<ParseObject> query = ParseObject.GetQuery("TestBigData").WhereEqualTo("objectId", "A04WgJ976D");
  97.  
  98.         var queryTask = query.FirstAsync();
  99.         while (!queryTask.IsCompleted) yield return null;
  100.  
  101.         ParseObject obj = queryTask.Result;
  102.  
  103.         ParseFile pfile = obj.Get<ParseFile>("file");
  104.         Debug.Log(pfile.Url.AbsoluteUri);
  105.         var imageRequest = new WWW(pfile.Url.AbsoluteUri);
  106.         yield return imageRequest;
  107.         Debug.Log("imageRequest " + imageRequest.text);
  108.     }
  109.  
  110. }
Add Comment
Please, Sign In to add comment