图像未显示在视图中 [英] Image not show in view

查看:108
本文介绍了图像未显示在视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在视图中显示图像,但它不显示。以下是我的代码



我尝试过:



查看代码是,



i am trying to show an image in view but it is not show. below is my code

What I have tried:

view code is,

<div>
<img src="data:image/jpeg;base64,@Convert.ToBase64String(Model.Photo)" />
</div>





和服务器端代码





and server side code is

public ActionResult getimage()
        {
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString) SqlDataAdapter da = new SqlDataAdapter("select top(1) photo from ParentImages where id=" + 1 + "", con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    var imageData = (byte[])dt.Rows[0]["photo"];
DataModel dm=new DataModel();
dm.Photo=imageData;
    return View(dm);
}

推荐答案

首先检查数据库中的图像 - 你不应该连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

插入图像的代码很可能是做同样的事情,这就是造成问题的原因。请参阅此处:为什么我会得到一个参数无效。我从数据库中读取图像时出现异常? [ ^ ]
Start by checking the images in your DB - you should never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
It's very likely that the code to insert the images is doing the same thing, and that is what is causing your problem. See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]


这篇关于图像未显示在视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆