Bmob后端云数据存储使用问题记录(一) ------ 下载数据表中的文件
数据服务文档地址 :
其实呢,Bmob官方文档已经非常完善了,只是...还是在使用的过程中还是会遇到问题的~
以后整Demo都想用这个,所以记录一下遇到的问题~
一.建表,增加文件列,上传文件
首先建立表,增加文件列
上传文件 :
二.查找并下载
unity中建立对应数据类(注意,名字一定要与列的名字保持一致,因为这个错误,卡了好久) :
public class BmobData : BmobTable {
//名字保持一致
public BmobFile File { get; set; }
public override void readFields(BmobInput input)
{
base.readFields(input);
this.File = input.Get<BmobFile>("File");
}
public override void write(BmobOutput output, bool all)
{
base.write(output, all);
output.Put("File", this.File);
}
}
得到地址:
public class BmobQueryS : MonoBehaviour {
public BmobUnity bmobUnity;
BmobQuery query = new BmobQuery();
public string TABLENAME = "Resource";
public bool isGetAll = false;
public Dictionary<string, string> typeToURL = new Dictionary<string, string>();
public List<String> Type = new List<String>();
public void Awake()
{
QueryFile();
}
public void QueryFile()
{
bmobUnity.Find<BmobData>(TABLENAME, query, (resp, exception) =>
{
if (exception != null)
{
print("查询失败, 失败原因为: " + exception.Message);
return;
}
//对返回结果进行处理
List<BmobData> list = resp.results;
foreach (BmobData bmobData in list)
{
typeToURL.Add(bmobData.File.filename, bmobData.File.url);
Debug.Log(bmobData.File.filename + " " + bmobData.File.getPath());
}
isGetAll = true;
foreach (string type in TypeAndFunc.Keys)
{
StartCoroutine(DownTool._Instance.LoadHotFixAssembly(type, typeToURL[type]);
}
});
}
public void IsGetAll(string type)
{
if (isGetAll)
{
StartCoroutine(DownTool._Instance.LoadHotFixAssembly(type,typeToURL[type]));
}
else
{
Type.Add(type);
}
}
}
下载并保存:
public class DownTool {
static DownTool instance;
public static DownTool _Instance
{
get
{
if (instance == null)
instance = new DownTool();
return instance;
}
}
public IEnumerator LoadHotFixAssembly(string filename,string uri)
{
UnityWebRequest request = UnityWebRequest.Get(uri);
yield return request.SendWebRequest();
CreatFile(Application.persistentDataPath,filename, request.downloadHandler.data);
}
public void CreatFile(string savePath, string name, byte[] content)
{
string filePath = savePath + "/" + name;
if (File.Exists(filePath))
{
File.Delete(filePath);
}
Stream sw;
FileInfo fileInfo = new FileInfo(filePath);
sw = fileInfo.Create();
sw.Write(content, 0, content.Length);
sw.Close();
sw.Dispose();
Debug.Log("保存文件路径:" + filePath);
}
}