当前位置: 首页 > news >正文

温州网站建设专业的公司seo兼职论坛

温州网站建设专业的公司,seo兼职论坛,毕节市住房和城乡建设局网站,在线制作假亲子鉴定书数据库存取图片并在MVC3中显示在View中 根据路径读取图片#xff1a;byte[] img System.IO.File.ReadAllBytes(d:\xxxx.jpg); 简介#xff1a;在有些情况下需要将图片转换为二进制流存放在数据库中#xff0c;当显示时再从数据库中读出来显示在界面上。 本文简…数据库存取图片并在MVC3中显示在View中 根据路径读取图片 byte[] img System.IO.File.ReadAllBytes(d:\xxxx.jpg); 简介在有些情况下需要将图片转换为二进制流存放在数据库中当显示时再从数据库中读出来显示在界面上。 本文简单介绍数据库中图片的存取方法并在MVC3中显示在Razor视图中。仅供初学者参考学习。 1. 将图片转换为二进制流 /// summary/// convert a picture file to byte array /// /summary public byte[] GetBytesFromImage(string filename) { FileStream fs new FileStream(filename,FileMode.Open,FileAccess.Read); int length (int)fs.Length; byte[] image new byte[length]; fs.Read(image, 0, length); fs.Close(); return image; }2. 将二进制文件写入数据库 /// summary/// write byte array to database/// /summarypublic void StoreImageToDB(byte[] image){ string connectionString Data Source.;Initial CatalogMyDB;User Idsa;Password123456; string strSql INSERT INTO TestImage(image) Values(image);using (SqlConnection connection new SqlConnection(connectionString)) { SqlCommand cmd new SqlCommand(strSql,connection); cmd.Parameters.Add(image, SqlDbType.Image).Value image; connection.Open();cmd.ExecuteNonQuery(); cmd.Clone(); }}3. 从数据库中读取图片 /// summary/// get image from database/// /summarypublic byte[] GetBytesFromDB(){ string connectionString Data Source.;Initial CatalogMyDB;User Idsa;Password123456; string strSql SELECT top 1 image FROM TestImage;using (SqlConnection connection new SqlConnection(connectionString)) { SqlCommand cmd new SqlCommand(strSql,connection); connection.Open();SqlDataReader reader cmd.ExecuteReader(); byte[] temp null; if (reader.Read()) { temp (byte[])reader.GetValue(0); } return temp; }}   4. 在Controller中添加返回图片的方法 /// summary/// Action that return the image file/// /summarypublic FileResult Image(){ //get image from database byte[] image GetBytesFromDB();//return the image to View return new FileContentResult(image, image/jpeg);//or like below //MemoryStream mem new MemoryStream(image, 0, image.Length); //return new FileStreamResult(mem, image/jpg);} 5. 在View中显示图片, 将src指定为我们返回图片的Action方法 img alt src/Home/Image / 上面的方法都是我们自己实现且用SQL语句存取数据库其实.NET框架已经给我们封装好了 很多现成的类再加上 EF 存取数据库可以使我们的代码变得更加 elegant。  1. 前台上传图片 using (Html.BeginForm(Edit, Admin, FormMethod.Post, new { enctype multipart/form-data })) {divUpload new image: input typefile nameImage //divinput typesubmit valueSave /}   它相当于 webform 中的 form action/Admin/Edit enctypemultipart/form-data methodpost enctype multipart/form-data 告诉浏览器将我们的文件流 post 到后台。 2. 将图片存入数据库 [HttpPost]public ActionResult Edit(Product product, HttpPostedFileBase image) {if (ModelState.IsValid) {if (image ! null) {product.ImageMimeType image.ContentType;product.ImageData new byte[image.ContentLength];image.InputStream.Read(product.ImageData, 0, image.ContentLength);}// save the product repository.SaveProduct(product);return RedirectToAction(Index);} else {// there is something wrong with the data valuesreturn View(product);}}   MVC框架会自动封装实例化我们的实体类和文件流并传到 post 方法中。 其中 HttpPostedFileBase 是一个抽象类实际传过来的对象 是它的子类 HttpPostedFileWrapper 对象。 HttpPostedFileBase 类定义了很多操作文件流的属性和接口。 3. 在 view 中请求显示图片的 action img srcUrl.Action(GetImage, Product, new { Model.ProductID }) / 其中读取图片流的方法如下 public FileContentResult GetImage(int productId) {Product prod repository.Products.FirstOrDefault(p p.ProductID productId);if (prod ! null) {return File(prod.ImageData, prod.ImageMimeType);} else {return null;}}   其中 FileContentResult  是 ActionResult 的子类 action 的返回类型有很多种它们都继承自抽象类 ActionResult 。 转载于:https://www.cnblogs.com/weiweithe/p/4363458.html
http://www.zqtcl.cn/news/258321/

相关文章:

  • 网站页头图片怎么做几个版面的网站
  • 网站 f型网站建设 大公司
  • 做网站最好选什么语言百度域名服务器
  • 网站维护一般多久西宁的网站建设
  • 网站建设需要什么工具投诉百度最有效的电话
  • 做家政网站公司策划公司英文
  • 自己建设个人网站要花费多少自己怎么制作微信网页链接
  • 邢台网站设计哪家专业php图书管理系统网站开发
  • 怎么去建一个网站艺术设计专业
  • 中国优秀设计网站有哪些内容万能影视免费观看app
  • 网站做响应式还是移动端广告创意设计模板
  • 企业网站建设的要求标准营销型网站定做价格
  • 兰溪优秀高端网站设计郑州正规网站制作公司
  • 霸气业务网站源码网站建设运营服务公司
  • 做seo对网站推广有什么作用网站开发程序流程图
  • 旅游网站怎么建设网站开发+搜索
  • 三分钟做网站传统企业建设营销型网站
  • 必须在当地网站备案化妆品做备案的网站
  • 网站建设7个主要流程图创建全国文明城市工作要求
  • 张店网站开发招聘怎样做网站变手机软件
  • 聊城做网站的公司流程网页设计网站模板
  • 宿迁网站建设哪家专业做网站宁夏
  • 静态网站规范贵州城乡建设厅施工员报名网站
  • 湖北长安建设集团股份有限公司网站wordpress主题套用
  • 本地门户网站系统小米应用商店
  • 网站建设怎么用宁波建设网网点
  • 购物网站二级页面模板国家企业信用公示信息系统官网app
  • tp框架做餐饮网站快速建站教程网
  • php自己做网站wordpress下拉刷新
  • 怎么提高网站收录量专业网站优化公司