123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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;
- MsgHandler.AddListener("AAAA", HandleMsg);
- Msg msg = new Msg("akdfakdf", new object());
- MsgHandler.SendMsg("AAA", msg)
- }
- CallBacke(isDownLoad);
- }
- }
- private void HandleMsg(Msg msg)
- {
- try
- {
-
- }
- catch (System.Exception e)
- {
-
- }
- }
- private void OnDestroy()
- {
-
- if (webRequest != null && !webRequest.isDone)
- {
- webRequest.Abort();
- }
- }
- }
|