Виктор Шейко 4 лет назад
Родитель
Сommit
a01a7b2413

+ 5 - 1
Assets/Scripts/Controllers/FileBrowserController.cs

@@ -10,6 +10,7 @@ using UnityEngine.Networking;
 using System.IO;
 using System;
 using Newtonsoft.Json;
+using System.Text.RegularExpressions;
 
 public class FileBrowserController : MonoBehaviour, IPointerDownHandler
 {
@@ -88,7 +89,10 @@ public class FileBrowserController : MonoBehaviour, IPointerDownHandler
             var deserialize = JsonConvert.DeserializeObject<List<urls>>(url);
             file_name = deserialize[0].file_name;
             Debug.Log($"file image {file_name}");
-            byte[] bytes = Encoding.UTF8.GetBytes(deserialize[0].base64);
+            var base64 = Regex.Match(deserialize[0].base64, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;//match.Groups["type"].Value;
+            //string base64 = deserialize[0].base64.Substring(deserialize[0].base64.IndexOf(',') + 1);
+            //base64 = base64.Trim('\0');
+            byte[] bytes = Convert.FromBase64String(base64);// Encoding.UTF8.GetBytes(deserialize[0].base64);
             Debug.Log($"bytes {bytes.Length}");
             form.AddBinaryData("file", bytes, deserialize[0].file_name);
             php = $"{Application.streamingAssetsPath}/upload.php";

+ 17 - 8
Assets/StandaloneFileBrowser/Plugins/StandaloneFileBrowser.jslib

@@ -35,31 +35,40 @@ var StandaloneFileBrowserWebGLPlugin = {
             this.value = null;
         };
         fileInput.onchange = function (event) {
-            // multiselect works
-            var urls = [];  
+            // multiselect works          
             
             for (var i = 0; i < event.target.files.length; i++) {
                 //urls.push(URL.createObjectURL(event.target.files[i]));                            
                 var reader = new FileReader();
                 reader.readAsDataURL(event.target.files[i]);
+                var url = URL.createObjectURL(event.target.files[i]);
+                var f_name= event.target.files[i].name;
                 var base64data;
-                reader.onload = function () {                    
+                reader.onload = function () {  
+                    var urls = [];                    
                     base64data = reader.result;
-                    console.log(base64data);                     
+                    console.log(base64data); 
+                    urls.push({
+                        "url": url,
+                        "file_name": f_name,
+                        "base64":  base64data
+                    });  
+                    //console.log('js: '+JSON.stringify(urls)); 
+                    SendMessage(gameObjectName, methodName, JSON.stringify(urls));                     
                   };
                   reader.onerror = function (error) {
                     console.log('Error: ', error);
-                    base64data= error;
+                    //base64data= error;
                   };
               
-                     urls.push({
+                     /*urls.push({
                         "url": URL.createObjectURL(event.target.files[i]),
                         "file_name": event.target.files[i].name,
                         "base64":  base64data
-                    });          
+                    });         */ 
                 }
             // File selected
-            SendMessage(gameObjectName, methodName, JSON.stringify(urls));
+            //SendMessage(gameObjectName, methodName, JSON.stringify(urls));
 
             // Remove after file selected
             document.body.removeChild(fileInput);