用www类进行上传和下载
private string path; //地址
//上传图片为例
public IEnumerator Post()
{
Texture2D tex = rawIma ;/获取图片
byte[] bytes = tex.EncodeToJPG();
string[] keys = new string[3];
keys[0] = "post";
keys[1] = "key";
keys[2] = "des";
object[] value = new object[3];
value[0] = bytes;
value[1] = "dpl";
value[2] = "kkk";
WWWForm form = new WWWForm();
for (int i = 0; i < keys.Length; i++)
{
if (value[i] == null)
{
throw new Exception("value " + i + " is null");
}
else
{
if (value[i].GetType() == typeof(string))
{
form.AddField(keys[i], value[i].ToString());
}
else if (value[i].GetType() == typeof(byte[]))
{
form.AddBinaryData(keys[i], (byte[])value[i]);
}
else
{
throw new Exception("value " + i + " 无效");
}
}
}
WWW w = new WWW(path, form);
yield return w;
if (w.error != null)
{
print(w.error);
}
else
{
PhotoQR str = JsonUtility.FromJson<PhotoQR>(w.text);// 拿到返回的数据
}
//下载数据
public IEnumerator GetFile()
{
WWW ww = new WWW(path);
yield return ww;
if (ww.isDone)
{
byte[] bytes = ww.bytes;
Debug.Log(Encoding.UTF8.GetString(bytes));
}
}
//定义类型
[Serializable]
public class PhotoQR
{
public string num;
public string qrcode;
public string url;
}