如何保存图像的BLOB到SQL数据库,而无需将其保存到文件系统中第一个? [英] How do I save the BLOB of an image into a sql database without saving it to the filesystem first?

查看:185
本文介绍了如何保存图像的BLOB到SQL数据库,而无需将其保存到文件系统中第一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出是如何,我可以从服务器的内存中存储的图像到我的数据库的BLOB没有它先保存到文件系统,那么直接。

我使用SQL服务器和除其他形式的信息予有2个图像需要被存储在数据库中。我也想知道我可以读出来,并将其转换回图像。

在分贝我有缩略图,它的类型是形象。如果我没有错,应该是正确的。

有关的图片上传我用下面的ASP控制:

 < ASP:文件上传ID =_ imageUpload=服务器/>
 

我从来没有做过这样的事,因为我很新的使用数据库尤其是与网站。

哦,对不起,如果这个问题已经被问和已经回答了。

在此先感谢!

我的整个code:

 保护无效_uploadImageBtn_Click(对象发件人,EventArgs的)
{
    字符串的扩展;

    //如果文件存在检查
    如果(!_imageUpload.HasFile)
    {
        _resultLbl.Text =请选择一个文件!;
        返回;
    }

    //检查文件扩展名
    延长= System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();

    如果(extension.Equals()及!JPG。;&安培;!extension.Equals(JPEG。)及&安培;!extension.Equals(。PNG))
    {
        _resultLbl.Text =只有图像文件(.JPGs和.PNGs)是允许的。
        返回;
    }

    //检查,如果图像尺寸是有效的
    如果(!ValidateFileDimensions(140,152))
    {
        _resultLbl.Text =允许的最大尺寸为:宽1520px和高度< = 140px。
        返回;
    }

    INT fileLen;
    字符串displayString =;

    //获取文件的长度。
    fileLen = _imageUpload.PostedFile.ContentLength;

    //创建一个字节数组来保存文件的内容。
    byte []的输入=新的字节[fileLen  -  1];
    输入= _imageUpload.FileBytes;

    //字节数组复制到一个字符串。
    对于(INT循环1 = 0;循环1< fileLen;循环1 ++)
    {
        displayString = displayString +输入[循环1]的ToString();
    }

    尝试
    {

        SqlConnection的sqlCn =新的SqlConnection(数据源=本地主机;初始目录=数据库;用户ID =用户;密码= PW);

        字符串QRY =INSERT INTO项目(缩略图)VALUES(@thumbnail);

        SqlCommand的sqlCom =新的SqlCommand(QRY,sqlCn);

        sqlCom.Parameters.Add(@缩略图,SqlDbType.Image,input.Length)。价值=输入;

        sqlCn.Open();
        sqlCom.ExecuteNonQuery();
        sqlCn.Close();

    }

    赶上(例外)
    {
        (...)
    }
}

公共BOOL ValidateFileDimensions(INT A高,INT aWidth)
{
    使用(System.Drawing.Image对象图像= System.Drawing.Image.FromStream(_imageUpload.PostedFile.InputStream))
    {
        返程(image.Height == A高和放大器;&安培; image.Width == aWidth);
    }
}
 

解决方案

您可以保存的返回的从字节数组 FileUpload.FileBytes

 如果(_imageUpload.HasFile)
{
 byte []的为imageData = _imageUpload.FileBytes;

 使用(SqlConnection的sqlCn =新的SqlConnection(服务器=本地主机;数据库=数据库名; UID =用户名; PWD =密码))
  {
    字符串QRY =INSERT INTO项目(缩略图)VALUES(@thumbnail);
    使用(SqlCommand的sqlCom =新的SqlCommand(QRY,sqlCn))
     {
       sqlCom.Parameters.Add(@缩略图,
                              SqlDbType.Image,
                              imageData.Length).value的=为imageData;
       sqlCn.Open();
       sqlCom.ExecuteNonQuery();
       sqlCn.Close();
     }
   }
}
 

编辑:

 保护无效_uploadImageBtn_Click(对象发件人,EventArgs的)
{
    字符串的扩展;

    //如果文件存在检查
    如果(!_imageUpload.HasFile)
    {
        _resultLbl.Text =请选择一个文件!;
        返回;
    }

    //检查文件扩展名
    延长= System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();

    如果(extension.Equals()及!JPG。;&安培;!extension.Equals(JPEG。)及&安培;!extension.Equals(。PNG))
    {
        _resultLbl.Text =只有图像文件(.JPGs和.PNGs)是允许的。
        返回;
    }

    //检查,如果图像尺寸是有效的
    如果(!ValidateFileDimensions(140,152))
    {
        _resultLbl.Text =允许的最大尺寸为:宽1520px和高度< = 140px。
        返回;
    }


    byte []的输入= _imageUpload.FileBytes;
    SqlConnection的sqlCn =新的SqlConnection(数据源=本地主机;初始
                                 目录=数据库;用户ID =用户;密码= PW);

    字符串QRY =INSERT INTO项目(缩略图)VALUES(@thumbnail);
    SqlCommand的sqlCom =新的SqlCommand(QRY,sqlCn);
    sqlCom.Parameters.Add(@缩略图,SqlDbType.Image,input.Length)。价值=输入;
    sqlCn.Open();
    sqlCom.ExecuteNonQuery();
    sqlCn.Close();
}
 

what I've been trying to find out is how I can store the BLOB of an image into my database without saving it to the filesystem first, so directly from the server's memory.

I use an sql server and among other form information I have 2 images that need to be stored in the database. I would also like to know how I can read them out and convert them back to images.

In the db I have "Thumbnail" which is of type "image". That should be correct if I'm not wrong.

For the image upload I use the following asp control:

<asp:FileUpload ID="_imageUpload" runat="server" />

I have never done anything like this as I am quite new to working with databases especially together with websites.

Oh, and sorry if this question has been asked and answered already.

Thanks in advance!

[edit]

My entire code:

protected void _uploadImageBtn_Click(object sender, EventArgs e)
{
    string extension;

    // checks if file exists
    if (!_imageUpload.HasFile)
    {
        _resultLbl.Text = "Please, Select a File!";
        return;
    }

    // checks file extension
    extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();

    if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png"))
    {
        _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed.";
        return;
    }

    // checks if image dimensions are valid
    if (!ValidateFileDimensions(140, 152))
    {
        _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px.";
        return;
    }

    int fileLen;
    string displayString = "";

    // Get the length of the file.
    fileLen = _imageUpload.PostedFile.ContentLength;

    // Create a byte array to hold the contents of the file.
    byte[] input = new byte[fileLen - 1];
    input = _imageUpload.FileBytes;

    // Copy the byte array to a string.
    for (int loop1 = 0; loop1 < fileLen; loop1++)
    {
        displayString = displayString + input[loop1].ToString();
    }

    try
    {

        SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=pw");

        string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";

        SqlCommand sqlCom = new SqlCommand(qry, sqlCn);

        sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input;

        sqlCn.Open();
        sqlCom.ExecuteNonQuery();
        sqlCn.Close();

    }

    catch (Exception)
    {
        (...)
    }
}

public bool ValidateFileDimensions(int aHeight, int aWidth)
{
    using (System.Drawing.Image image = System.Drawing.Image.FromStream(_imageUpload.PostedFile.InputStream))
    {
        return (image.Height == aHeight && image.Width == aWidth);
    }
}

解决方案

You can save the returned byte array from FileUpload.FileBytes.

if(_imageUpload.HasFile)
{
 byte[] imageData = _imageUpload.FileBytes;

 using(SqlConnection sqlCn = new SqlConnection("Server=localhost;database=databaseName;uid=userName;pwd=password"))
  {
    string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";
    using(SqlCommand sqlCom = new SqlCommand(qry, sqlCn))
     {
       sqlCom.Parameters.Add("@thumbnail",
                              SqlDbType.Image,
                              imageData.Length).Value=imageData;
       sqlCn.Open();
       sqlCom.ExecuteNonQuery();
       sqlCn.Close();
     }
   }
}

EDIT:

protected void _uploadImageBtn_Click(object sender, EventArgs e)
{
    string extension;

    // checks if file exists
    if (!_imageUpload.HasFile)
    {
        _resultLbl.Text = "Please, Select a File!";
        return;
    }

    // checks file extension
    extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();

    if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png"))
    {
        _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed.";
        return;
    }

    // checks if image dimensions are valid
    if (!ValidateFileDimensions(140, 152))
    {
        _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px.";
        return;
    }


    byte []input = _imageUpload.FileBytes;
    SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial 
                                 Catalog=database;User ID=user;Password=pw");

    string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";
    SqlCommand sqlCom = new SqlCommand(qry, sqlCn);
    sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input;
    sqlCn.Open();
    sqlCom.ExecuteNonQuery();
    sqlCn.Close();
}

这篇关于如何保存图像的BLOB到SQL数据库,而无需将其保存到文件系统中第一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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