FileBrowserController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Text;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.EventSystems;
  8. using SFB;
  9. using UnityEngine.Networking;
  10. using System.IO;
  11. using System;
  12. using Newtonsoft.Json;
  13. public class FileBrowserController : MonoBehaviour, IPointerDownHandler
  14. {
  15. //public enum Extensions { Image, VuforiaDB }; //".png, .jpg"".xml, .dat"
  16. //public Extensions extensions = Extensions.Image;
  17. public InputField inputField;
  18. //public Button OpenVuforiaXML;
  19. //public Button OpenImage;
  20. // Warning: paths returned by FileBrowser dialogs do not contain a trailing '\' character
  21. // Warning: FileBrowser can only show 1 dialog at a time
  22. #if UNITY_WEBGL && !UNITY_EDITOR
  23. //
  24. // WebGL
  25. //
  26. [DllImport("__Internal")]
  27. private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
  28. public void OnPointerDown(PointerEventData eventData) {
  29. UploadFile(gameObject.name, "OnFileUpload", ".png, .jpg", false);
  30. Debug.Log("OnPointerDown");
  31. }
  32. // Called from browser
  33. public void OnFileUpload(string url) {
  34. StartCoroutine(UpLoadUserData(url));
  35. }
  36. #else
  37. //
  38. // Standalone platforms & editor
  39. //
  40. public void OnPointerDown(PointerEventData eventData) { }
  41. void Start()
  42. {
  43. // OpenVuforiaXML.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat")); });
  44. // OpenImage.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор изображения", new ExtensionFilter("Image Files", "png", "jpg", "jpeg")); });
  45. var button =GetComponent<Button>();
  46. button.onClick.AddListener(OnClick);
  47. }
  48. //public void OpenFileBrowser()
  49. //{
  50. // ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat"));
  51. //}
  52. #endif
  53. public void OnClick()
  54. {
  55. // Open file
  56. string[] paths = new string[0];
  57. // Open file with filter
  58. //var es = new[] {new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
  59. //es = new[] {new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
  60. paths = StandaloneFileBrowser.OpenFilePanel($"Выбор изображения", "", "png,jpg", false);
  61. //var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions, true);
  62. // Open file async
  63. //StandaloneFileBrowser.OpenFilePanelAsync(name_view, "", es, false, (string[] paths) => {
  64. // if (paths.Length > 0)
  65. // {
  66. // StartCoroutine(OutputRoutine(new System.Uri(paths[0]).AbsoluteUri));
  67. // }
  68. //});
  69. //var paths = StandaloneFileBrowser.OpenFilePanel(name_view, "", "txt", true);
  70. if (paths.Length > 0)
  71. {
  72. Debug.Log(paths[0]);
  73. //StartCoroutine(UpLoadUserData(new System.Uri(paths[0]).AbsoluteUri));
  74. StartCoroutine(UpLoadUserData(paths[0]));
  75. }
  76. }
  77. private IEnumerator OutputRoutine(string url)
  78. {
  79. //var loader = new WWW(url);
  80. Debug.Log($"file url pc {url}");
  81. var server_url = $"{Application.streamingAssetsPath}/Targets/{Path.GetFileName(url)}";
  82. Debug.Log($"file url server {server_url}");
  83. var request = UnityWebRequest.Get(url);
  84. yield return request.SendWebRequest();
  85. //yield return loader;
  86. if (!File.Exists(url))
  87. {
  88. while (!request.isDone)
  89. {
  90. yield return null;
  91. }
  92. if (!string.IsNullOrEmpty(request.error))
  93. {
  94. Debug.LogError("Error unpacking:" + request.error + " path: " + request.url);
  95. yield break; //skip it
  96. }
  97. else
  98. {
  99. if (!Directory.Exists(Path.GetDirectoryName(server_url)))
  100. {
  101. Directory.CreateDirectory(Path.GetDirectoryName(server_url));
  102. }
  103. File.WriteAllBytes(server_url, request.downloadHandler.data);
  104. Debug.Log($"success file load {server_url}");
  105. inputField.text = server_url;
  106. //could add to some kind of uninstall list?
  107. }
  108. }
  109. yield return 0;
  110. //output.texture = loader.texture;
  111. }
  112. /// <summary>
  113. /// Отправка файла на сервер
  114. /// </summary>
  115. /// <param name="url"></param>
  116. /// <returns></returns>
  117. IEnumerator UpLoadUserData(string url)
  118. {
  119. //var file = UnityWebRequest.Get(url);
  120. //yield return file;
  121. Debug.Log(url);
  122. WWWForm form = new WWWForm();
  123. //form.AddBinaryData("file", file.downloadHandler.data);
  124. //var screenShotURL = URL_OF_SERVER_PHP_PAGE;
  125. //var bytes = Encoding.UTF8.GetBytes(url);
  126. //var form = new WWWForm();
  127. // string data = JsonUtility.ToJson(DataBaseManager.DataBase[username]);
  128. //WWWForm form = new WWWForm();
  129. //form.AddField("name", username);
  130. //form.AddField("data", data);
  131. UnityWebRequest www;
  132. string file_name;
  133. if (Application.platform == RuntimePlatform.WebGLPlayer)
  134. {
  135. var deserialize = JsonConvert.DeserializeObject<List<urls>>(url);
  136. file_name = deserialize[0].file_name;
  137. byte[] bytes = Encoding.UTF8.GetBytes(deserialize[0].reader);
  138. form.AddBinaryData("file", bytes, deserialize[0].file_name);
  139. www = UnityWebRequest.Post($"{Application.streamingAssetsPath}/upload.php", form);
  140. }
  141. else
  142. {
  143. file_name = Path.GetFileName(url);
  144. var bytes = File.ReadAllBytes(url);
  145. form.AddBinaryData("file", bytes, file_name);
  146. www = UnityWebRequest.Post($"http://dev.prmsys.net/positionviewer/StreamingAssets/upload.php", form);
  147. }
  148. yield return www.SendWebRequest();
  149. if (www.isHttpError)
  150. Debug.Log(www.error);
  151. else
  152. {
  153. Debug.Log("Uploaded");
  154. inputField.text = $"{Application.streamingAssetsPath}/{file_name}";
  155. }
  156. Debug.Log(www.downloadHandler.text);
  157. }
  158. class urls
  159. {
  160. public string url { get; set; }
  161. public string file_name { get; set; }
  162. public string reader { get; set; }
  163. }
  164. }