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 InputField inputField;
-
-
-
-
- #if UNITY_WEBGL && !UNITY_EDITOR
-
-
-
- [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");
- }
-
- public void OnFileUpload(string url) {
- StartCoroutine(UpLoadUserData(url));
- }
- #else
-
-
-
- public void OnPointerDown(PointerEventData eventData) { }
- void Start()
- {
-
-
- var button =GetComponent<Button>();
- button.onClick.AddListener(OnClick);
- }
-
-
-
-
- #endif
- public void OnClick()
- {
-
- string[] paths = new string[0];
-
-
-
- paths = StandaloneFileBrowser.OpenFilePanel($"Выбор изображения", "", "png,jpg", false);
-
-
-
-
-
-
-
-
-
-
- if (paths.Length > 0)
- {
- Debug.Log(paths[0]);
-
- StartCoroutine(UpLoadUserData(paths[0]));
- }
- }
- private IEnumerator OutputRoutine(string 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();
-
- 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;
- }
- 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;
-
- }
- }
- yield return 0;
-
- }
-
-
-
-
-
- IEnumerator UpLoadUserData(string url)
- {
-
-
- Debug.Log(url);
-
- WWWForm form = new WWWForm();
-
-
-
-
-
-
-
-
- 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; }
- }
- }
|