如何在不使用ASP.NET MVC4中的实体框架的情况下从数据库检索图像

编程入门 行业动态 更新时间:2024-10-25 22:31:32
本文介绍了如何在不使用ASP.NET MVC4中的实体框架的情况下从数据库检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是ASP.NET MVC的新手,我需要您的帮助.我正在尝试创建插入并从数据库中获取图像,我可以将图像保存为二进制格式,但无法将其恢复为图像格式

I'm new to ASP.NET MVC and I need some help from you. I'm trying to create insert and get image from database and I can save an image into binary format but I can't get it back into image format

我的输出如下图

这是我的观点

@model IEnumerable<HelphoProject.Models.Property> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> hi you are now in Index page of HElpho </p> <div> @Html.DropDownList("CityID", (IEnumerable<SelectListItem>)ViewBag.CityList,"Select City") </div> <div> @Html.DropDownList("propertyTypeID",(IEnumerable<SelectListItem>)ViewBag.PropertyTypeList,"Select PropertyType") </div> @Html.ActionLink("Add New Property", "AddNewProperty",null, new { @class="btn btn-primary"}) <table class="table" style="width: 1200px;"> <tr> <th> <b>ImageID</b> </th> <th> <b>CityID</b> </th> <th> <b>TypeID</b> </th> <th> <b>Image</b> </th> <th> <b>Description</b> </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(a =>item.ImageID ) </td> <td> @Html.DisplayFor(a=>item.CityID) </td> <td> @Html.DisplayFor(a=>item.propertyTypeID) </td> <td style="width: 500px;"> @Html.Raw(item.Image) </td> <td> <img src="/Content/RetrieveImage/@item.ImageID" alt="" height=100 width=200 /> </td> <td> @Html.DisplayFor(a=>item.Description) </td> <td style="width: 500px;"> @*@Html.Raw(item.content)*@ </td> <td> </td> <td> @* @Html.DisplayFor(modelItem => item.Description)*@ </td> </tr> } </table>

这是我的控制器代码:

using HelphoProject.DataAccessLayer; using HelphoProject.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace HelphoProject.Controllers { public class PropertyController : Controller { // // GET: /Property/ [HttpGet] public ActionResult Index() { Cities(); getPropertyType(); readwriteProperty writedata = new readwriteProperty(); List<Property> propertyList = writedata.getproperties(); var content = propertyList.Select(item => new Property() { ImageID = item.ImageID, CityID = item.CityID, propertyTypeID = item.propertyTypeID, Image = item.Image, Description = item.Description }); List<Property> contentModel = content.Select(item => new Property() { ImageID = item.ImageID, CityID = item.CityID, propertyTypeID = item.propertyTypeID, Image = item.Image, Description = item.Description }).ToList(); RetrieveImage(1); return View(contentModel); } public ActionResult RetrieveImage(int id) { byte[] cover = GetImageFromDataBase(id); if (cover != null) { return File(cover, "image/jpg"); } else { return null; } } /// <summary> /// /// </summary> /// <param name="Id"></param> /// <returns></returns> public byte[] GetImageFromDataBase(int Id) { readwriteProperty writedata = new readwriteProperty(); byte[] Image = writedata.getImageFromDB(); return Image; } public ActionResult Cities() { readwriteCity dbconnection = new readwriteCity(); List<City> pcontent = new List<City>(); { //Get all page content from TblHome table pcontent = dbconnection.getAllCities(); }; List<SelectListItem> cityList = new List<SelectListItem>(); //List<string> items = new List<string>(); foreach (var item in pcontent) { cityList.Add(new SelectListItem { Text = item.CityName, Value = item.CityID.ToString() }); } ViewBag.CityList = cityList; return View(); } public ActionResult getPropertyType() { readwritePropertyType dbconnection = new readwritePropertyType(); List<PropertyType> pcontent = new List<PropertyType>(); { // Get all page content from TblHome table pcontent = dbconnection.getAllPropertyType(); }; List<SelectListItem> propertyTypeList = new List<SelectListItem>(); List<string> items = new List<string>(); foreach (var item in pcontent) { propertyTypeList.Add(new SelectListItem { Text = item.propertyType, Value = item.propertyTypeID.ToString() }); } ViewBag.PropertyTypeList = propertyTypeList; return View(); } public ActionResult AddNewProperty() { Cities(); getPropertyType(); return View(); } [HttpPost] public ActionResult AddNewProperty(Property objProperty) { HttpPostedFileBase file = Request.Files["ImageData"]; readwriteProperty writedata = new readwriteProperty(); string str = writedata.SaveProperty(file, objProperty); if (str == "Data Saved Successfully") { return RedirectToAction("Index"); } return View(objProperty); } } }

这是我的模特:

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace HelphoProject.Models { public class Property { public int ImageID { get; set; } public int CityID { get; set; } public int propertyTypeID { get; set; } [Required] public byte[] Image { get; set; } public string Description { get; set; } } }

我又用一件事连接数据库

And I have used one more thing to connect with database

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; using System.Data; using System.Configuration; using HelphoProject.Models; using System.IO; namespace HelphoProject.DataAccessLayer { public class readwriteProperty { SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString); SqlCommand cmd; SqlDataAdapter sda; DataTable dt; public string SaveProperty(HttpPostedFileBase file, Property objproperty) { objproperty.Image = ConvertToBytes(file,null); cmd = new SqlCommand("spSaveProperty", cn); cmd.CommandType = CommandType.StoredProcedure; // cmd.Parameters.AddWithValue("@intStateID", objCity.StateID); cmd.Parameters.AddWithValue("@ImageID", objproperty.ImageID); cmd.Parameters.AddWithValue("@CityID", objproperty.CityID); cmd.Parameters.AddWithValue("@TypeID", objproperty.propertyTypeID); cmd.Parameters.AddWithValue("@Image", objproperty.Image); cmd.Parameters.AddWithValue("@Description", objproperty.Description); cn.Open(); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { return "Datat Not Saved Successfully"; } cn.Close(); return "Data Saved Successfully"; } public byte[] getImageFromDB() { cn.Open(); sda = new SqlDataAdapter("select Image FROM tblImages", cn); dt = new DataTable(); sda.Fill(dt); Property objmainProperty = new Property(); foreach (DataRow dr in dt.Rows) { Property objProperty = new Property(); objProperty.Image=(byte[])dr["Image"]; objmainProperty.Image = objProperty.Image; } cn.Close(); return objmainProperty.Image; } public List<Property> getproperties() { cn.Open(); sda = new SqlDataAdapter("select ImageID,CityID,TypeID,Image,Description from tblImages", cn); dt = new DataTable(); sda.Fill(dt); List<Property> properties = new List<Property>(); foreach(DataRow dr in dt.Rows) { Property objProperty = new Property(); objProperty.ImageID = Convert.ToInt16(dr["ImageID"]); objProperty.CityID = Convert.ToInt16(dr["CityID"]); objProperty.propertyTypeID = Convert.ToInt16(dr["TypeID"]); objProperty.Image = (byte[])dr["Image"]; objProperty.Description = dr["Description"].ToString(); properties.Add(objProperty); } cn.Close(); return properties; } public byte[] ConvertToBytes(HttpPostedFileBase image,Property objproperty) { if (image != null) { byte[] imageBytes = null; BinaryReader reader = new BinaryReader(image.InputStream); imageBytes = reader.ReadBytes((int)image.ContentLength); return imageBytes; } else { byte[] imageBytes = objproperty.Image; BinaryReader reader = new BinaryReader(image.InputStream); imageBytes = reader.ReadBytes((int)image.ContentLength); return imageBytes; } } } }

使用此代码,我可以将图像上传并保存到数据库中,并且可以从数据中获取所有数据.我也可以获取图像,但是以二进制格式,我认为您在观看下面的图像后可以了解更多信息

Using this code I can upload and save my images in to database and I can get all data from data. I can get image also but in binary format I think you can understand more after watching below image

推荐答案

您使用了错误的模型名称.更改

You have used the wrong model name. Change

img src="/Content/RetrieveImage/@item.ImageID" alt="" height=100 width=200

img src="/Property/RetrieveImage/@item.ImageID" alt="" height=100 width=200

在视图部分.

更多推荐

如何在不使用ASP.NET MVC4中的实体框架的情况下从数据库检索图像

本文发布于:2023-11-15 04:23:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   情况下   图像   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!