FileBrowserController.cs 4.0 KB

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