123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System.Text;
- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using SFB;
- using UnityEngine.Networking;
- using System.IO;
- using System;
- using Newtonsoft.Json;
- public class FileBrowserController : MonoBehaviour, IPointerDownHandler
- {
- //public enum Extensions { Image, VuforiaDB }; //".png, .jpg"".xml, .dat"
- //public Extensions extensions = Extensions.Image;
- public InputField inputField;
- //public Button OpenVuforiaXML;
- //public Button OpenImage;
- // Warning: paths returned by FileBrowser dialogs do not contain a trailing '\' character
- // Warning: FileBrowser can only show 1 dialog at a time
- #if UNITY_WEBGL && !UNITY_EDITOR
- //
- // WebGL
- //
- [DllImport("__Internal")]
- private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
- public void OnPointerDown(PointerEventData eventData) {
- UploadFile(gameObject.name, "OnFileUpload", ".png, .jpg", false);
- Debug.Log("OnPointerDown");
- }
- // Called from browser
- public void OnFileUpload(string url) {
- StartCoroutine(UpLoadUserData(url));
- }
- #else
- //
- // Standalone platforms & editor
- //
- public void OnPointerDown(PointerEventData eventData) { }
- void Start()
- {
- // OpenVuforiaXML.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat")); });
- // OpenImage.onClick.AddListener(() => { ShowLoadDialogCoroutine($"Выбор изображения", new ExtensionFilter("Image Files", "png", "jpg", "jpeg")); });
- var button =GetComponent<Button>();
- button.onClick.AddListener(OnClick);
- }
- //public void OpenFileBrowser()
- //{
- // ShowLoadDialogCoroutine($"Выбор объекта", new ExtensionFilter("Vuforia Files", "xml", "dat"));
- //}
- #endif
- public void OnClick()
- {
- // Open file
- string[] paths = new string[0];
- // Open file with filter
- //var es = new[] {new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
- //es = new[] {new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
- paths = StandaloneFileBrowser.OpenFilePanel($"Выбор изображения", "", "png,jpg", false);
-
- //var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions, true);
- // Open file async
- //StandaloneFileBrowser.OpenFilePanelAsync(name_view, "", es, false, (string[] paths) => {
- // if (paths.Length > 0)
- // {
- // StartCoroutine(OutputRoutine(new System.Uri(paths[0]).AbsoluteUri));
- // }
- //});
- //var paths = StandaloneFileBrowser.OpenFilePanel(name_view, "", "txt", true);
- if (paths.Length > 0)
- {
- Debug.Log(paths[0]);
- //StartCoroutine(UpLoadUserData(new System.Uri(paths[0]).AbsoluteUri));
- StartCoroutine(UpLoadUserData(paths[0]));
- }
- }
- private IEnumerator OutputRoutine(string url)
- {
- //var loader = new WWW(url);
- Debug.Log($"file url pc {url}");
- var server_url = $"{Application.streamingAssetsPath}/Targets/{Path.GetFileName(url)}";
- Debug.Log($"file url server {server_url}");
- var request = UnityWebRequest.Get(url);
- yield return request.SendWebRequest();
- //yield return loader;
- if (!File.Exists(url))
- {
- while (!request.isDone)
- {
- yield return null;
- }
- if (!string.IsNullOrEmpty(request.error))
- {
- Debug.LogError("Error unpacking:" + request.error + " path: " + request.url);
- yield break; //skip it
- }
- else
- {
- if (!Directory.Exists(Path.GetDirectoryName(server_url)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(server_url));
- }
- File.WriteAllBytes(server_url, request.downloadHandler.data);
- Debug.Log($"success file load {server_url}");
- inputField.text = server_url;
- //could add to some kind of uninstall list?
- }
- }
- yield return 0;
- //output.texture = loader.texture;
- }
- /// <summary>
- /// Отправка файла на сервер
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- IEnumerator UpLoadUserData(string url)
- {
- //var file = UnityWebRequest.Get(url);
- //yield return file;
- Debug.Log(url);
-
- WWWForm form = new WWWForm();
- //form.AddBinaryData("file", file.downloadHandler.data);
- //var screenShotURL = URL_OF_SERVER_PHP_PAGE;
- //var bytes = Encoding.UTF8.GetBytes(url);
- //var form = new WWWForm();
- // string data = JsonUtility.ToJson(DataBaseManager.DataBase[username]);
- //WWWForm form = new WWWForm();
- //form.AddField("name", username);
- //form.AddField("data", data);
- UnityWebRequest www;
- string file_name;
- if (Application.platform == RuntimePlatform.WebGLPlayer)
- {
- var deserialize = JsonConvert.DeserializeObject<List<urls>>(url);
- file_name = deserialize[0].file_name;
- byte[] bytes = Encoding.UTF8.GetBytes(deserialize[0].reader);
- form.AddBinaryData("file", bytes, deserialize[0].file_name);
- www = UnityWebRequest.Post($"{Application.streamingAssetsPath}/upload.php", form);
- }
- else
- {
- file_name = Path.GetFileName(url);
- var bytes = File.ReadAllBytes(url);
- form.AddBinaryData("file", bytes, file_name);
- www = UnityWebRequest.Post($"http://dev.prmsys.net/positionviewer/StreamingAssets/upload.php", form);
- }
- yield return www.SendWebRequest();
- if (www.isHttpError)
- Debug.Log(www.error);
- else
- {
- Debug.Log("Uploaded");
- inputField.text = $"{Application.streamingAssetsPath}/{file_name}";
- }
- Debug.Log(www.downloadHandler.text);
- }
- class urls
- {
- public string url { get; set; }
- public string file_name { get; set; }
- public string reader { get; set; }
- }
- }
|