UpLoadmageItem.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Agora.Rtc.LitJson;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class UpLoadmageItem : MonoBehaviour
  9. {
  10. public string path;
  11. string uuid ;
  12. public JsonData fildData ;
  13. public void init(Texture2D texture2D,string str)
  14. {
  15. this.transform.GetComponent<RawImage>().texture = texture2D;
  16. string filePath = str;
  17. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
  18. uuid = fileNameWithoutExtension+"_"+GetFormattedDateTime();// Guid.NewGuid().ToString();
  19. string fileName = uuid+".png";
  20. string objecName = "workorder/"+int.Parse(ChuLiItemManager.ChooseData["id"].ToString())+"/";
  21. // 文件预上传
  22. JsonData jData = new JsonData();
  23. byte[] data = texture2D.EncodeToPNG();
  24. jData["size"] = data.Length;
  25. jData["ext"] = "jpg";
  26. jData["name"] = fileName;
  27. JsonData fileData = new JsonData();
  28. fileData["filePath"] = filePath;
  29. fileData["fileSize"] = data.Length;
  30. fileData["fileName"] = fileName;
  31. // project /{ projectId}/ inspection /{ projectInspectionId}/{ taskId}
  32. fileData["objectName"] = objecName;
  33. fildData = fileData;
  34. this.path = objecName+fileName;
  35. }
  36. string GetFormattedDateTime()
  37. {
  38. DateTime now = DateTime.Now;
  39. return now.ToString("yyyyMMddHHmm");
  40. // 或者更明确的写法:
  41. // return $"{now.Year}{now.Month:D2}{now.Day:D2}{now.Hour:D2}{now.Minute:D2}";
  42. }
  43. public void startUpLoad(Action<bool> uploadcallback)
  44. {
  45. // 文件上传
  46. DownLoadXRManager.PutFile(fildData["filePath"].ToString(), fildData["fileName"].ToString(), fildData["objectName"].ToString() + fildData["fileName"].ToString(),uuid, int.Parse(fildData["fileSize"].ToString()), msg =>
  47. {
  48. uploadcallback.Invoke(true);
  49. Debug.Log(" 文件上传成功 " + msg);
  50. });
  51. }
  52. public void delete()
  53. {
  54. BaoGaoUpLoadManager.Instance.itemlist.Remove(this);
  55. Destroy(this.transform.GetComponent<RawImage>().texture);
  56. Destroy(this.gameObject);
  57. }
  58. }