public object upload() { var path = System.IO.Directory.GetCurrentDirectory(); string[] result = new string[this.Request.Form.Files.Count]; int i = 0; if (this.connection.State != ConnectionState.Open) { this.connection.Open(); } var trans = this.connection.BeginTransaction(); try { foreach (var key in this.Request.Form.Files) { model.upload tmp = new model.upload(); tmp.filename = key.FileName; byte[] buffer = new byte[key.Length]; key.OpenReadStream().Read(buffer, 0, buffer.Length); System.IO.File.WriteAllBytes(path + "/data/image/" + tmp.id, buffer); result[i++] = tmp.id; this.connection.ExecuteScalar(tmp.Insert()); } trans.Commit(); } catch (Exception e) { this.log.Error(e.Message); trans.Rollback(); return(new { msg = "", code = 2 }); } return(new { msg = string.Join(',', result), code = 1 }); }
private string[] downloadImage(string[] serverid) { var path = System.IO.Directory.GetCurrentDirectory(); string[] result = new string[serverid.Length]; int i = 0; HttpClient client = new HttpClient(); if (this.db.State != ConnectionState.Open) { this.db.Open(); } var trans = this.db.BeginTransaction(); try { Task.Run(async() => { foreach (var key in serverid) { if (string.IsNullOrEmpty(key)) { result[i++] = key; continue; } model.upload tmp = new model.upload(); string url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + this.token + "&media_id=" + key; var dataset = await client.GetByteArrayAsync(url); System.IO.File.WriteAllBytes(path + "/data/image/" + tmp.id, dataset); result[i++] = tmp.id; this.db.Execute(tmp.Insert()); } trans.Commit(); }).Wait(); return(result); } catch (Exception e) { trans.Rollback(); return(new string[5]); } }