【问题标题】:C# (Asp.net MVC 2) Display image from byte[] in ViewC# (Asp.net MVC 2) 在视图中显示来自字节 [] 的图像
【发布时间】:2013-08-02 13:31:41
【问题描述】:

我需要将图片保存在我的 asp.net mvc 应用程序的数据库中。我在表 MimeType (nvarchar[50]) 和 ImageData 中创建了一个字段,用于将图片保存在 byte[] 中。我使用 ado.net。我将图像保存在这样的表格中:

 private DBAppl db = new DBAppl();
            public void CreateNewCar(newcar Car, HttpPostedFileBase image) 
            {
                    if (image != null)
                        {

                            Car.mimetype = image.ContentType;
                            Car.ImageData = new byte[image.ContentLength];
                            image.InputStream.Read(Car.ImageData, 0, image.ContentLength);                    

                        }
                db.AddTonewcars(Car);
                db.SaveChanges();
            } 

图片正常保存在表格中。然后我想在 View 中显示我的图像。我在控制器中创建方法

public FileContentResult GetImage(int newcarid)
        {
            DBAppl db = new DBAppl();
            newcar car = (from p in db.newcars
                              where p.newcarid == newcarid
                               select p).First();
            return File(car.ImageData, car.mimetype.Trim());
        }

鉴于我插入了这段代码:

<% if (Model.ImageData == null)
{ %>
None
<% }
else
{ %>
<img src="<%= Url.Action("GetImage", "Cars", new { Model.newcarid }) %>" alt="<%: Model.description %>" /> <% } %> 

但是图片没有加载,只有alt。求助,我做错了什么?我尝试在 html 页面的源代码中使用链接,但读取该图片有错误。我查看了 mozilla 的“关于页面的信息”,看到该页面有我的图片(778 kb),但它是 0px x 0px。

【问题讨论】:

    标签: c# asp.net asp.net-mvc image binary


    【解决方案1】:

    在返回文件之前尝试设置标题

    HttpContext.Response.AddHeader("Content-Length"("Content-Type", "image/jpeg"));
    

    或者您正在访问您的标题。

    将图片/jpeg 用于 jpg 文件

    google 其他扩展和文件 tpes。

    【讨论】:

      【解决方案2】:

      我是这样解决问题的:

      public FileContentResult GetImage(int newcarid)
              {
                  DBAppl db = new DBAppl();
                  newcar car = (from p in db.newcars
                                    where p.newcarid == newcarid
                                     select p).First();
                  return File(car.ImageData**.ToArray()**, car.mimetype.Trim());
              }
      

      在课堂上:

       public void CreateNewCar(newcar Car, HttpPostedFileBase image) 
                  {
                  **var car = new newcar();**
                  if (image != null)
                      {
      
                          car.name = Car.name;
                          car.date = DateTime.Now;
                          car.color = Car.color;
                          car.picmimetype = image.ContentType;
      
                          int length = image.ContentLength;
                          byte[] buffer = new byte[length];
                          image.InputStream.Read(buffer, 0, length);
                          car.ImageData = buffer;
      
                      }
                  db.AddTonewcars(car);
              db.SaveChanges();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 2013-06-01
        • 2019-07-30
        相关资源
        最近更新 更多