protected void Page_Load(object sender, EventArgs e) { try { //if (this.Request.HttpMethod != "POST") //{ // this.Response.Write("Request method must be POST"); // return; //} // handle as image //string device = this.Request.Form["deviceid"].ToString(); using (SmartCityEntities ctx = new SmartCityEntities()) { SampledImage sImage = new SampledImage(); sImage.device_id = Convert.ToInt32(this.Request.QueryString["device"]); sImage.image_timestamp = DateTime.Now; System.IO.BufferedStream bufRead = new System.IO.BufferedStream(this.Request.Files[0].InputStream); System.IO.BinaryReader binaryRead = new System.IO.BinaryReader(bufRead); sImage.imagesample = binaryRead.ReadBytes(this.Request.Files[0].ContentLength); ctx.SampledImage.Add(sImage); ctx.SaveChanges(); } return; } catch (Exception ex) { this.Response.Write(ex.ToString()); return; } }
protected void Page_Load(object sender, EventArgs e) { try { if (this.Request.HttpMethod != "GET") { this.Response.Write("Request method must be GET"); return; } this.Response.ContentType = "multipart/form-data"; using (SmartCityEntities ctx = new SmartCityEntities()) { int deviceid = Convert.ToInt32(this.Request.QueryString["device"].ToString()); SampledImage sImage = ctx.SampledImage.OrderByDescending(pimg => pimg.id_images).FirstOrDefault(devid => devid.device_id == deviceid); if (sImage != null) { this.Response.ContentType = "image/jpg"; this.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.jpg", deviceid)); this.Response.OutputStream.Write(sImage.imagesample, 0, sImage.imagesample.Count()); return; } } } catch (Exception ex) { this.Response.Write(ex.ToString()); return; } }