FileBrowserController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. using System.Text.RegularExpressions;
  14. public class FileBrowserController : MonoBehaviour, IPointerDownHandler
  15. {
  16. //public enum Extensions { Image, VuforiaDB }; //".png, .jpg"".xml, .dat"
  17. //public Extensions extensions = Extensions.Image;
  18. public InputField inputField;
  19. //public Button OpenVuforiaXML;
  20. //public Button OpenImage;
  21. // Warning: paths returned by FileBrowser dialogs do not contain a trailing '\' character
  22. // Warning: FileBrowser can only show 1 dialog at a time
  23. #if UNITY_WEBGL && !UNITY_EDITOR
  24. //
  25. // WebGL
  26. //
  27. [DllImport("__Internal")]
  28. private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
  29. public void OnPointerDown(PointerEventData eventData) {
  30. UploadFile(gameObject.name, "OnFileUpload", ".png, .jpg", false);
  31. Debug.Log("OnPointerDown");
  32. }
  33. // Called from browser
  34. public void OnFileUpload(string url) {
  35. StartCoroutine(UpLoadUserData(url));
  36. }
  37. #else
  38. //
  39. // Standalone platforms & editor
  40. //
  41. public void OnPointerDown(PointerEventData eventData) { }
  42. void Start()
  43. {
  44. // OpenVuforiaXML.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat")); });
  45. // OpenImage.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор изображения", new ExtensionFilter("Image Files", "png", "jpg", "jpeg")); });
  46. var button =GetComponent<Button>();
  47. button.onClick.AddListener(OnClick);
  48. }
  49. //public void OpenFileBrowser()
  50. //{
  51. // ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat"));
  52. //}
  53. #endif
  54. public void OnClick()
  55. {
  56. // Open file
  57. string[] paths = new string[0];
  58. paths = StandaloneFileBrowser.OpenFilePanel($"Выбор изображения", "", "png,jpg", false);
  59. if (paths.Length > 0)
  60. {
  61. Debug.Log(paths[0]);
  62. StartCoroutine(UpLoadUserData(paths[0]));
  63. }
  64. }
  65. /// <summary>
  66. /// Отправка файла на сервер
  67. /// </summary>
  68. /// <param name="url"></param>
  69. /// <returns></returns>
  70. IEnumerator UpLoadUserData(string url)
  71. {
  72. Debug.Log(url);
  73. WWWForm form = new WWWForm();
  74. UnityWebRequest www;
  75. string file_name;
  76. string php = "";
  77. if (Application.platform == RuntimePlatform.WebGLPlayer)
  78. {
  79. var deserialize = JsonConvert.DeserializeObject<List<urls>>(url);
  80. file_name = deserialize[0].file_name;
  81. Debug.Log($"file image {file_name}");
  82. var base64 = Regex.Match(deserialize[0].base64, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;//match.Groups["type"].Value;
  83. //string base64 = deserialize[0].base64.Substring(deserialize[0].base64.IndexOf(',') + 1);
  84. //base64 = base64.Trim('\0');
  85. byte[] bytes = Convert.FromBase64String(base64);// Encoding.UTF8.GetBytes(deserialize[0].base64);
  86. Debug.Log($"bytes {bytes.Length}");
  87. form.AddBinaryData("file", bytes, deserialize[0].file_name);
  88. php = $"{Application.streamingAssetsPath}/upload.php";
  89. }
  90. else
  91. {
  92. file_name = Path.GetFileName(url);
  93. using (FileStream fstream = File.OpenRead(url))
  94. {
  95. // преобразуем строку в байты
  96. byte[] bytes = new byte[fstream.Length];
  97. fstream.Read(bytes, 0, bytes.Length);
  98. form.AddBinaryData("file", bytes, file_name);//{Path.GetExtension(url)}
  99. php = $"http://dev.prmsys.net/positionviewer/StreamingAssets/upload.php";
  100. }
  101. }
  102. www = UnityWebRequest.Post(php, form);
  103. yield return www.SendWebRequest();
  104. if (www.isHttpError)
  105. Debug.Log(www.error);
  106. else
  107. {
  108. Debug.Log("Uploaded");
  109. inputField.text = $"{Application.streamingAssetsPath}/{file_name}";
  110. }
  111. Debug.Log(www.downloadHandler.text);
  112. }
  113. class urls
  114. {
  115. public string url { get; set; }
  116. public string file_name { get; set; }
  117. public string base64 { get; set; }
  118. }
  119. }