Unity--上传下载文件并保存到本地
Posted 格拉格拉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity--上传下载文件并保存到本地相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 上传文件到服务器
/// </summary>
public void UpFileToServer() {
//StartCoroutine("UpLoadFiles");
StartCoroutine("UpLoadFile");
}
public IEnumerator UpLoadFile()
{
byte[] file = File.ReadAllBytes(localPath);
string cont = Utils.Instance.Encrypt(file);
Debug.LogError("...cont:"+cont);
JsonData jdata = new JsonData();
//jdata["userid"] = PlayerPrefs.GetInt("userid", 0);
jdata["userid"] = 4;
jdata["file"] = cont;
UnityWebRequest req = new UnityWebRequest("http://aaa.vvv.cn/Api/Home/Upload", "POST");
req.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
byte[] postBytes = Encoding.Default.GetBytes(jdata.ToJson());
req.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
yield return req.SendWebRequest();
//if (req.responseCode == 200)
//{
// string text1 = req.downloadHandler.text;
// Debug.LogError("...返回信息:" + text1);
//}
if (req.isHttpError || req.isNetworkError)
{
Debug.LogError(req.error);
}
else
{
string text2 = req.downloadHandler.text;
Debug.LogError("上传完成...返回信息:" + text2);
}
}
1.传参下载
/// <summary>
/// 下载文件到本地
/// </summary>
public void CheckFriendFile(int friendId) {
JsonData jdata = new JsonData();
//jdata["userid"] = PlayerPrefs.GetInt("userid", 0);
jdata["userid"] = friendId;
StartCoroutine(DownFilePath("http://aaa.vvv.cn/Api/Home/Down", jdata));
}
IEnumerator DownFilePath(string _url, JsonData data)
{
UnityWebRequest request = new UnityWebRequest(_url, "POST");
byte[] postBytes = Encoding.UTF8.GetBytes(data.ToJson());
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
request.SetRequestHeader("CLEARANCE", "YOUR_TARGET_SERVER");
request.timeout = 5;
yield return request.SendWebRequest();
if (request.responseCode == 200)
{
string text = request.downloadHandler.text;
Debug.LogError("下载内容:"+text);
JsonData jData = JsonMapper.ToObject(text);
int backCode = int.Parse(jData["code"].ToString());
switch (backCode) {
case 1:
if (request.isDone)
{
byte[] bytes = Utils.Instance.Decrypt(jData["data"].ToString());
CreatFile(friendPath, bytes);
}
break;
default:
string _info = jData["info"].ToString();
Debug.LogError("查询失败:" + _info);
break;
}
}
}
2.直接下载
IEnumerator DownFile(string _url) {
UnityWebRequest request = UnityWebRequest.Get(_url);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
yield return request.SendWebRequest();
if (request.responseCode == 200)
{
string text = request.downloadHandler.text;
Debug.LogError("下载内容:" + text);
if (request.isDone)
{
byte[] bytes = request.downloadHandler.data;
CreatFile(friendPath, bytes);
}
}
if (request.isHttpError || request.isNetworkError)
{
Debug.LogError(request.error);
}
else
{
Debug.LogError(file + "下载完成");
}
}
3.存本地
void CreatFile(string path, byte[] bytes)
{
//文件流信息
//StreamWriter sw;
Stream sw;
file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
//如果此文件存在则打开
//sw = file .Append();
//如果此文件不存在则创建
sw = file.Create();
//以行的形式写入信息
//sw.WriteLine(info);
sw.Write(bytes, 0, bytes.Length);
sw.Close();
sw.Dispose();
}
以上是关于Unity--上传下载文件并保存到本地的主要内容,如果未能解决你的问题,请参考以下文章