12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- public class DownLoadItem : MonoBehaviour
- {
-
-
-
- public string downLoadPath;
-
-
-
- public bool isDownLoad;
-
-
-
- public byte[] downLoadData;
-
-
-
- public float progress;
-
-
-
- public string downLoadState;
- private UnityWebRequest webRequest;
- private string baseurl = "http://office.ghz-tech.com:9904/api";
- public void Init(string downLoadPath, Action<bool> CallBacke)
- {
- this.downLoadPath = downLoadPath;
- StartCoroutine(DownloadFile(CallBacke));
- }
- private System.Collections.IEnumerator DownloadFile(Action<bool> CallBacke)
- {
- string url = baseurl + "/file/download";
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
- {
- byte[] bodyRaw = Encoding.UTF8.GetBytes(downLoadPath);
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
- webRequest.SendWebRequest();
- while (!webRequest.isDone)
- {
-
- progress = webRequest.downloadProgress;
-
- yield return null;
- }
- if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError)
- {
- Debug.LogError("Download Failed: " + webRequest.error);
-
- downLoadState = webRequest.error;
- isDownLoad = false;
- }
- else
- {
- downLoadState = "下载成功";
- downLoadData = webRequest.downloadHandler.data;
- isDownLoad = true;
-
- }
- CallBacke(isDownLoad);
- }
- }
- private void OnDestroy()
- {
-
- if (webRequest != null && !webRequest.isDone)
- {
- webRequest.Abort();
- }
- }
- }
|