Bmob後端雲資料儲存使用問題記錄(一) ------ 下載資料表中的檔案
阿新 • • 發佈:2018-12-25
資料服務文件地址 : http://doc.bmob.cn/data/csharp/index.html
其實呢,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); } }