/// <summary> /// Upload files to "a_attaches" table. Before files been saved, all files attached to /// the recid will been deleted. (This is supported by the semantic-DA samantics configuration) /// <p><b>FIXME:</b></p> /// this method used 2 round of memory copy, should implemented in stream style /// </summary> /// <param name="files"></param> /// <param name="busiTbl">business table to which file is attached, e.g. "a_users"</param> /// <param name="recid">business record Id to which file is owned, e.g. "admin"</param> public void AttachFiles(List <string> files, string busiTbl, string recid, Action <MsgCode, AnsonResp> onOk = null, Action <MsgCode, AnsonResp> onErr = null) { AnsonMsg jmsg = Delete(null, "a_attaches"); AnUpdateReq del = (AnUpdateReq)jmsg.Body(0); del.WhereEq("busiTbl", busiTbl) .WhereEq("busiId", recid); foreach (string file in files) { byte[] f = File.ReadAllBytes(file); string fn = Path.GetFileName(file); string b64 = AESHelper.Encode64(f); del.Post(AnInsertReq .formatInsertReq(null, null, "a_attaches") .Cols("attName", "busiId", "busiTbl", "uri") .Nv("attName", fn) .Nv("busiId", recid) .Nv("busiTbl", busiTbl) .Nv("uri", b64)); } jmsg.Header(Header()); Console(jmsg); Commit(jmsg, (code, data) => { if (MsgCode.ok == code.code) { if (onOk != null) { onOk(code, (AnsonResp)data.Body(0)); } else { Utils.Logi(code.ToString()); } } else if (onErr != null) { onErr(code, (AnsonResp)data.Body(0)); } else { Utils.Warn(data.ToString()); } }, onErr: (c, err) => { if (onErr != null) { onErr(c, err); } else { Utils.Warn(string.Format(@"code: {0}, error: {1}", c, err.Msg())); } }); }
public AnsonMsg Insert(string uri, string tbl, string[] act = null) { AnInsertReq itm = AnInsertReq.formatInsertReq(null, tbl, uri); AnsonMsg jmsg = UserReq(uri, new Port(Port.insert), act, itm); AnsonHeader header = new AnsonHeader(ssInf.ssid, ssInf.uid); if (act != null && act.Length > 0) { header.act(act); } return(jmsg.Header(header) .Body(itm, uri)); }
static void UploadTransaction(CancellationTokenSource waker, AnsonClient client, string p) { // string p = Path.get(filename); byte[] f = File.ReadAllBytes(p); string b64 = AESHelper.Encode64(f); AnsonMsg jmsg = client.Update(null, "a_users"); AnUpdateReq upd = (AnUpdateReq)jmsg.Body(0); upd.Nv("nationId", "CN") .WhereEq("userId", "admin") // .post(((UpdateReq) new UpdateReq(null, "a_attach") .Post(AnUpdateReq.formatDelReq(null, null, "a_attaches") .WhereEq("busiTbl", "a_users") .WhereEq("busiId", "admin") .Post((AnInsertReq.formatInsertReq(null, null, "a_attaches") .Cols("attName", "busiId", "busiTbl", "uri") .Nv("attName", "-Anclient.cs Test") // The parent pk can't be resulved, we must provide the value. // See https://odys-z.github.io/notes/semantics/best-practices.html#fk-ins-cate .Nv("busiId", "admin") .Nv("busiTbl", "a_users") .Nv("uri", b64)))); jmsg.Header(client.Header()); client.Console(jmsg); client.Commit(jmsg, (code, data) => { if (MsgCode.ok == code.code) { Utils.Logi(code.ToString()); } else { Utils.Warn(data.ToString()); } }, onErr: (c, err) => { Assert.Fail(string.Format(@"code: {0}, error: {1}", c, err.Msg())); }, waker); }