Asp.net mvc MVC4ASP.net,使用文件上传代码如何将图像保存在数据库中

Asp.net mvc MVC4ASP.net,使用文件上传代码如何将图像保存在数据库中,asp.net-mvc,Asp.net Mvc,以下是我的观点 @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) { <table> <tr> <td>File :</td> <td><input type="file" name="File" id="file" /> </td>

以下是我的观点

     @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table> 
    <tr>

<td>File :</td>    
    <td><input type="file" name="File" id="file" /> </td>
  </tr>
  <tr> 
  <td><input type="submit" name="submit" value="upload" /></td>
  </tr>
public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Picture picture)
        {
            if (picture.File.ContentLength > 0)
            {
                var fileName = Path.GetFileName(picture.File.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/Images"), fileName);
                picture.File.SaveAs(path);
            }
            return RedirectToAction("Index");
        }


and Model:

namespace FileUpload.Models
{
    public class Picture
    {
        public HttpPostedFileBase File { get; set; }



    }
这段代码帮助我将图像保存在MVC项目根图像文件夹中,但我想将其保存到数据库中。我尝试了很多教程,但都没有成功
我正在制作学生表格,每个学生都会注册他的照片

将图像转换为字节,然后将其存储在数据库中

[HttpPost]
        public ActionResult Index(Picture picture)
        {
            byte[] Image;
            if (Request.Files["files"] != null)
            {
               using (var binaryReader = new BinaryReader(Request.Files["file"].InputStream))
                {
                   Image = binaryReader.ReadBytes(Request.Files["files"].ContentLength);
                }
               Picture.File =Image;
            }
            return RedirectToAction("Index");
        }
模型

用于显示图像的视图

if (Model.File != null)
  {
     string imageBase64 = Convert.ToBase64String(Model.File );
     string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
     <img src="@imageSrc" width="100" height="100" />
  }
if(Model.File!=null)
{
字符串imageBase64=Convert.tobase64字符串(Model.File);
string imageSrc=string.Format(“数据:image/gif;base64,{0}”,imageBase64);
}

您能给我一个解决方案吗?如何从数据库中检索这些图像并在“索引”视图中显示。这些代码不是我应该在“视图”模型中编写的吗??在我的视图中,模型编译器没有解析'@imageSrc'单词和另一个查询,当我编写上传方法时,编译器会自动转换'Picture model'字节[]作为静态…为什么会发生这种情况???@mgsdew实际上我不明白…你能发布你的代码吗?我的合作伙伴已经在堆栈溢出中发布了这个问题。请检查链接@mgsdew向我显示你从数据库检索图像的代码,并从模型中删除静态关键字
static byte[]image
if (Model.File != null)
  {
     string imageBase64 = Convert.ToBase64String(Model.File );
     string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
     <img src="@imageSrc" width="100" height="100" />
  }