Unity——文件上传 文件导入见往期


using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public static class UpLoadExcel
{
private static string filePath;
private static string fileName;


/// <summary>
/// 创建接口并发送
/// </summary>
public static void SendExcel()
{
//读取文件
SelectFileOrPath.GetInstance().SelectExcelFilePath_Windows();
filePath = SelectFileOrPath.GetInstance().SelectFilePath;
fileName = SelectFileOrPath.GetInstance().SelectFile;

//与服务器端相同的插口
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//链接服务器端的插口
IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 31, 30 });

IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 8840);

tcpClient.Connect(iPEndPoint);
Debug.Log("连接上服务器");

//-------------------------------------------------------------------------
//向服务器端发送文件
//string message = "im go";
tcpClient.Send(ConvertByte());

//接收服务器端的消息
byte[] data = new byte[1024];
int length = tcpClient.Receive(data);
string c = Encoding.UTF8.GetString(data, 0, length);
Debug.Log("接收到消息"+c);

//关闭插口
tcpClient.Close();
}

/// <summary>
/// 将文件转化为字节流
/// </summary>
/// <param name="fileUrl"></param>
/// <returns></returns>
public static byte[] ConvertByte()
{
FileStream fs = new FileStream(string.Format("{0},{1}",filePath,fileName), FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];

fs.Read(buffer, 0, buffer.Length);
fs.Close();
return buffer;
}
}

posted @ 2023-08-17 17:33  Trigger_F  阅读(54)  评论(0编辑  收藏  举报