简体   繁体   English

将图像保存到数据库以在asp.net中检索图像

[英]save image to database in retrive it in asp.net

How to save image multipal in sql server in byte format ,and retrieve it in image tool without using httphandlar also convert it to pdf. 如何在SQL Server中以字节格式保存图像multipal,并在不使用httphandlar的情况下在图像工具中检索图像,也将其转换为pdf。 please guid me the easy way . 请指导我简单的办法。

I tried: 我试过了:

if (FileUpload2.HasFile)
        {
            int length = FileUpload2.PostedFile.ContentLength;
            byte[] imgbyt = new byte[length];
            HttpPostedFile img = FileUpload2.PostedFile;
            img.InputStream.Read(imgbyt, 0, length);
            string filename = Path.GetFileName(FileUpload2.FileName);
            lbl_image.Text = filename;
            con.Open();
            cmd = new SqlCommand("update StudData set student_image = @image, image_name= @filename where userid='" + Session["Loginname"] + "'", con);
            cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyt;
            cmd.Parameters.Add("@filename", SqlDbType.VarChar, 50).Value = filename;
            int count = cmd.ExecuteNonQuery();
            con.Close();

        }

It store image in sql in byte format but How I retrieve it? 它以字节格式将图像存储在sql中,但如何检索? please suggest me easiest way 请建议我最简单的方法

Try this 尝试这个

Add an image tag on your aspx page 在aspx页面上添加图片标签

<img src='"<%=imageSrc%>"' />

Then on the code behind page get the Base64 of the image from the byte array that will come from the db. 然后在页面后面的代码中,从将来自数据库的字节数组中获取图像的Base64。

string imageSrc = string.Format("data:image/jpeg;base64,{0}", Convert.ToBase64String(bytes, 0, bytes.Length));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM