/// <summary> /// 判断指定路径是否存在 /// </summary> /// <param name="path">路径.</param> public static bool exists(string path) { #if UNITY_ANDROID && !UNITY_EDITOR bool isAppPath = false; if (path.StartsWith(app())) { path = path.Substring(app().Length + 1); isAppPath = true; } else if (!path.StartsWith("/")) { isAppPath = true; } if (isAppPath) { try { AndroidJavaObject curActivity = UNIEnv.getCurrentActivity(); AndroidJavaObject assetManager = curActivity.Call <AndroidJavaObject>("getAssets", new object[0]); AndroidJavaObject fd = assetManager.Call <AndroidJavaObject>("openFd", new object[1] { path }); if (fd != null) { return(true); } return(false); } catch (Exception ex) { return(false); } } else { if (File.Exists(path)) { return(true); } else { return(Directory.Exists(path)); } } #else if (File.Exists(path)) { return(true); } else { return(Directory.Exists(path)); } #endif }
/// <summary> /// 上传文件 /// </summary> /// <param name="fileParams">文件参数.</param> /// <param name="parameters">请求参数.</param> /// <param name="resultHandler">返回回调.</param> /// <param name="faultHandler">失败回调.</param> /// <param name="progressHandler">进度回调</param> public void upload( Dictionary <string, HTTPFile> fileParams, Dictionary <string, string> parameters, LuaFunction resultHandler, LuaFunction faultHandler, LuaFunction progressHandler) { _resultHandler = resultHandler; _faultHandler = faultHandler; _uploadProgressHandler = progressHandler; #if UNITY_ANDROID && !UNITY_EDITOR string appPath = luascriptcore.modules.foundation.Path.app(); AndroidJavaObject currentActivity = UNIEnv.getCurrentActivity(); AndroidJavaObject assetManager = currentActivity.Call <AndroidJavaObject> ("getAssets", new object[0]); #endif WWWForm form = new WWWForm(); if (fileParams != null) { foreach (KeyValuePair <string, HTTPFile> kv in fileParams) { HTTPFile file = kv.Value; string path = file.path; byte[] fileData = null; #if UNITY_ANDROID && !UNITY_EDITOR if (path.StartsWith(appPath) || !path.StartsWith("/")) { //应用包内文件 if (path.StartsWith(appPath)) { path = path.Substring(appPath.Length + 1); } AndroidJavaObject inputStream = assetManager.Call <AndroidJavaObject> ("open", path); using (MemoryStream ms = new MemoryStream()) { try { IntPtr buffer = AndroidJNI.NewByteArray(1024); jvalue[] args = new jvalue[1]; args [0].l = buffer; IntPtr readMethodId = AndroidJNIHelper.GetMethodID(inputStream.GetRawClass(), "read", "([B)I"); int hasRead = 0; while ((hasRead = AndroidJNI.CallIntMethod(inputStream.GetRawObject(), readMethodId, args)) != -1) { byte[] byteArray = AndroidJNIHelper.ConvertFromJNIArray <byte[]> (buffer); ms.Write(byteArray, 0, hasRead); } fileData = new byte[ms.Length]; ms.Read(fileData, 0, (int)ms.Length); } finally { ms.Close(); } } } else { #endif FileStream fs = new FileStream(path, FileMode.Open); try { fileData = new byte[fs.Length]; fs.Read(fileData, 0, (int)fs.Length); } finally { fs.Close(); } #if UNITY_ANDROID && !UNITY_EDITOR } #endif if (fileData != null) { form.AddBinaryData(kv.Key, fileData, System.IO.Path.GetFileName(file.path), file.mimeType); } } } if (parameters != null) { foreach (KeyValuePair <string, string> kv in parameters) { form.AddField(kv.Key, kv.Value); } } _request = UnityWebRequest.Post(url, form); if (_request != null) { fillHeaders(); sendRequest(); } }