将图像存储在数据库中

时间:2011-02-16 08:38:29

标签: .net asp.net

我有一个asp页面。在asp页面中我有一个图像控件。图像控件中有一个图像。我想将此图像保存在数据库中。在数据库中,特定的字段数据类型是图像。怎么可能?

1 个答案:

答案 0 :(得分:4)

查看此文章: Save and Retrieve Images from the Database using ASP.NET 2.0 and ASP.NET 3.5

上述文章中的示例

首先,您需要将图像转换为如下字节:

FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
    //To create a PostedFile
    HttpPostedFile File = imgUpload.PostedFile;
    //Create byte Array with file len
    imgByte = new Byte[File.ContentLength];
    //force the control to load data in array
    File.InputStream.Read(imgByte, 0, File.ContentLength);
}

然后在添加到数据库时使用imgByte作为值。

相关问题