79 lines
2.8 KiB
C#
79 lines
2.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace GeneralTools
|
|
{
|
|
public class UpLoadImage : SingletonBaseAttribute<UpLoadImage>
|
|
{
|
|
[Header("上传文件夹名")]
|
|
public string upLoadFolderName;
|
|
[Header("用户名")]
|
|
public string userStr;
|
|
[Header("密码")]
|
|
public string passwordStr;
|
|
/// <summary>
|
|
/// 上传的路径
|
|
/// </summary>
|
|
string pathForUpLoad;
|
|
/// <summary>
|
|
/// 获取的路径
|
|
/// </summary>
|
|
string pathForGetQR;
|
|
bool isReady = false;
|
|
|
|
public delegate void UpLoadFinishAction(Texture tex);
|
|
|
|
public override void IStart()
|
|
{
|
|
pathForUpLoad = @"https://www.fyzserver.com/photos/upload.php?folderName=" + upLoadFolderName;
|
|
pathForGetQR = @"https://www.fyzserver.com/photos/" + upLoadFolderName;
|
|
isReady = true;
|
|
}
|
|
public void UpLoad(byte[] bytes, string texName, Action<Texture> qrCallBack = null)
|
|
{
|
|
if (!isReady)
|
|
return;
|
|
StartCoroutine(UpLoadToServer(bytes, texName, pathForUpLoad, qrCallBack));
|
|
}
|
|
public void UpLoad(Texture2D texture2d, string texName, Action<Texture> qrCallBack = null)
|
|
{
|
|
byte[] bytes = texture2d.EncodeToJPG();
|
|
UpLoad(bytes, texName, qrCallBack);
|
|
}
|
|
public Texture GetQR(string texName)
|
|
{
|
|
if (!isReady)
|
|
return null;
|
|
return QRTool.GeneQRwithString1(pathForGetQR + "/" + texName + ".jpg", 256, 256);
|
|
}
|
|
protected IEnumerator UpLoadToServer(byte[] bytes, string texName, string spath, Action<Texture> qrCallBack)
|
|
{
|
|
if (string.IsNullOrEmpty(upLoadFolderName) || string.IsNullOrEmpty(userStr) || string.IsNullOrEmpty(passwordStr))
|
|
{
|
|
Debug.LogError("上传路径/用户名/密码 未设置");
|
|
yield break;
|
|
}
|
|
|
|
WWWForm wwwForm = new WWWForm();
|
|
wwwForm.AddField("user", userStr);
|
|
wwwForm.AddField("password", passwordStr);
|
|
wwwForm.AddField("photoName", texName);
|
|
wwwForm.AddBinaryData("photoData", bytes);
|
|
UnityWebRequest webRequest = UnityWebRequest.Post(spath, wwwForm);
|
|
yield return webRequest.SendWebRequest();
|
|
if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
{
|
|
Debug.Log(webRequest.error);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log(texName.ToString() + "发送成功");
|
|
qrCallBack?.Invoke(GetQR(texName));
|
|
}
|
|
}
|
|
}
|
|
} |