【问题标题】:save image to database in retrive it in asp.net将图像保存到数据库并在 asp.net 中检索
【发布时间】:2014-05-12 09:13:14
【问题描述】:

如何在sql server中以字节格式保存图像multipal,并在图像工具中检索而不使用httphandlar也将其转换为pdf。请指导我简单的方法。

我试过了:

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();

        }

它将图像以字节格式存储在 sql 中,但我如何检索它? 请建议我最简单的方法

【问题讨论】:

  • 最简单/高效?
  • 从 db 中获取图片并使用 Response.Write 将其写入页面
  • 无法分配 write 因为它的方法组

标签: asp.net


【解决方案1】:

试试这个

在您的 aspx 页面上添加图像标签

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

然后在页面后面的代码中,从来自数据库的字节数组中获取图像的 Base64。

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

【讨论】:

  • Convert.ToBase64String(bytes, 0, bytes.Length) 得到错误 byte convert.toBase64string(byte[],int,int) 不可能
  • 如何从数据库中读取? byte[] bytes= sdr["student-image"].tostring();..get error
  • 你能告诉我你做了什么来检索图像吗?也可以参考akadia.com/services/dotnet_read_write_blob.html
  • byte[] img = (byte[])sdr["student_image"]; string base64string = Convert.ToBase64String(img, 0, img.Length); lbl_image .Text += "";
  • 但它给出了无效 url 的错误。网址太长
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 2017-03-12
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多