Tz2/Assets/Zion/Scripts/配送/FileControllor.cs

335 lines
9.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Common;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class FileControllor : MonoBehaviour
{
public string mainString;
/// <summary>
/// 下载
/// </summary>
public Button btnSaveExcel;
/// <summary>
/// 上传
/// </summary>
public Button btnOpenExcel;
/// <summary>
/// 打开Excel
/// </summary>
public Button btnLoadExcel;
/// <summary>
///
/// </summary>
public bool isSave = false;
public string downloadNumber;
/// <summary>
///
/// </summary>
public string downloadUrl;
/// <summary>
/// 下载地址
/// </summary>
public string uploadExamData;
/// <summary>
/// 下载地址
/// </summary>
public string uploadUrl;
/// <summary>
/// 上传成功
/// </summary>
public GameObject UpTrue;
/// <summary>
/// 上传失败
/// </summary>
public GameObject UpFalse;
public string pathMain;
public UploadFileData uploadFileData = new UploadFileData();
void Start()
{
downloadUrl = "http://172.16.1.113:9000/file/biaodan/基建工程延期供应计划调整附件_附件1_供应计划制定.xlsx";
uploadUrl = "http://172.16.1.113:8083/member/pro/upload/uploadFileAndParam";
PlayerPrefs.SetString("ExcelNane", mainString);
btnSaveExcel.onClick.AddListener(() =>
{
SaveProject();
btnOpenExcel.gameObject.SetActive(true);
});
btnOpenExcel.onClick.AddListener(() =>
{
OpenProject();
//btnOpenExcel.gameObject.SetActive(false);
});
btnLoadExcel.onClick.AddListener(() =>
{
OpenExcelFile(pathMain);
});
}
List<string> zipFilePaths = new List<string>();
/// <summary>
/// 打开文件
/// </summary>
public void OpenProject()
{
OpenFileDlg pth = new OpenFileDlg();
pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);
//pth.filter = "xlsx (*.XLSX)";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = Application.dataPath; // default path
pth.title = "上传文件";
//pth.defExt = "XLSX";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (OpenFileDialog.GetOpenFileName(pth))
{
string filepath = pth.file;//选择的文件路径;
Debug.Log(filepath);
//Upload(filepath);
if (!zipFilePaths.Contains(filepath))
{
zipFilePaths.Add(filepath);
}
}
}
private void Update()
{
if (Input.GetKeyDown(key: KeyCode.F1))
{
Test_Zip();
}
}
/// <summary>
/// 下载Excel模板到本地
/// </summary>
public void SaveProject()
{
SaveFileDlg pth = new SaveFileDlg();
pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);
//pth.filter = "xlsx (*.XLSX)";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = Application.dataPath; // default path
pth.title = "保存文件";
pth.defExt = "XLSX";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (SaveFileDialog.GetSaveFileName(pth))
{
string filepath = pth.file;//选择的文件路径;
Debug.Log(filepath);
//StartCoroutine(DownloadFiles(filepath, downloadUrl));
Download(filepath);
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="url"></param>
/// <param name="path">文件地址</param>
/// <returns></returns>
IEnumerator UploadFile(string url, string path)
{
byte[] bytes = File.ReadAllBytes(path);
WWWForm formData = new WWWForm();
formData.AddBinaryData("file", bytes, System.IO.Path.GetFileName(path), "application/octet-stream");
formData.AddField("stuId", uploadFileData.stuId);
formData.AddField("appId", uploadFileData.appId);
formData.AddField("examId", uploadFileData.examId);
formData.AddField("examinationId", uploadFileData.examinationId);
UnityWebRequest request = UnityWebRequest.Post(url, formData);
request.SetRequestHeader("Authorization", uploadFileData.token);
Debug.Log("Authorization");
yield return request.SendWebRequest();
Debug.Log("SendWebRequest");
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log(request.error);
UpTrue.SetActive(false);
UpFalse.SetActive(true);
}
else
{
string GetStr = request.downloadHandler.text;
Debug.Log(GetStr);
JsonResponseExcel response = JsonConvert.DeserializeObject<JsonResponseExcel>(GetStr);
if (response.code == 200)
{
Debug.Log($"成功,{response.data.url}||{response.data.name}");
UpTrue.SetActive(true);
UpFalse.SetActive(false);
zipFilePaths.Clear();
//btnOpenExcel.gameObject.SetActive(false);
}
else
{
Debug.Log($"失败code 是 {response.code}: {response.msg}");
UpTrue.SetActive(false);
UpFalse.SetActive(true);
}
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="path"></param>
public void Upload(string path)
{
string url = uploadUrl;
uploadFileData.stuId = "123";
uploadFileData.appId = "456";
uploadFileData.examId = "789";
uploadFileData.examinationId = "101112";
uploadFileData.token = "token";
StartCoroutine(UploadFile(url, path));
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="url">地址</param>
/// <returns></returns>
IEnumerator DownloadFile(string path, string url)
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log(request.error);
}
else
{
File.WriteAllBytes(path, request.downloadHandler.data);
Debug.Log("文件下载完成");
pathMain = path;
btnSaveExcel.gameObject.SetActive(false);
btnLoadExcel.gameObject.SetActive(true);
OpenExcelFile(path);
isSave = true;
}
}
/// <summary>
/// 打开Excel文件
/// </summary>
/// <param name="filePath">文件地址</param>
public void OpenExcelFile(string filePath)
{
// 检查文件是否存在
if (File.Exists(filePath))
{
try
{
System.Diagnostics.Process.Start(filePath);
}
catch (System.Exception e)
{
UnityEngine.Debug.LogError("Error opening file: " + e.Message);
}
}
else
{
UnityEngine.Debug.LogError("File not found: " + filePath);
}
}
void Download(string path)
{
string url = downloadUrl;
StartCoroutine(DownloadFile(path, url));
}
#region Test_Zip
void Test_Zip()
{
string zipOutputPath = Application.streamingAssetsPath + "/zipTest.zip";
if (ZipWrapper.Zip(zipFilePaths, zipOutputPath, "", new ZipCallback()))
{
Upload(zipOutputPath);
}
}
public class ZipCallback : ZipWrapper.ZipCallback
{
public override bool OnPreZip(ZipEntry _entry)
{
Debug.Log("OnPreZip Name " + _entry.Name);
Debug.Log("OnPreZip IsFile" + _entry.IsFile);
return base.OnPreZip(_entry);
}
public override void OnPostZip(ZipEntry _entry)
{
Debug.Log("OnPostZip Name " + _entry.Name);
}
public override void OnFinished(bool _result)
{
Debug.Log("OnZipFinished _result " + _result);
}
}
#endregion
#region Test_UnZip
public class UnzipCallback : ZipWrapper.UnzipCallback
{
public override bool OnPreUnzip(ZipEntry _entry)
{
Debug.Log("OnPreUnzip Name " + _entry.Name);
Debug.Log("OnPreUnzip IsFile" + _entry.IsFile);
return base.OnPreUnzip(_entry);
}
public override void OnPostUnzip(ZipEntry _entry)
{
Debug.Log("OnPostUnzip Name " + _entry.Name);
base.OnPostUnzip(_entry);
}
public override void OnFinished(bool _result)
{
Debug.Log("OnUnZipFinished _result " + _result);
base.OnFinished(_result);
}
}
#endregion
}
public class JsonResponseExcel
{
public int code { get; set; }
public string msg { get; set; }
public ReceiveData data { get; set; }
}
public class ReceiveData
{
public string name { get; set; }
public string url { get; set; }
public string id { get; set; }
public string data { get; set; }
}
public class UploadFileData
{
public string stuId { get; set; }
public string appId { get; set; }
public string examId { get; set; }
public string examinationId { get; set; }
public string token { get; set; }
}