0

我一直试图找出的是如何将图像的 BLOB 存储到我的数据库中,而无需先将其保存到文件系统,因此直接从服务器的内存中。

我使用 sql server 和其他表单信息,我有 2 张图像需要存储在数据库中。我还想知道如何将它们读出并将它们转换回图像。

在数据库中,我有“图像”类型的“缩略图”。如果我没有错,那应该是正确的。

对于图像上传,我使用以下 asp 控件:

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

我从来没有做过这样的事情,因为我对使用数据库非常陌生,尤其是与网站一起使用。

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

提前致谢!

[编辑]

我的整个代码:

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);
    }
}
4

1 回答 1

4

您可以将返回的字节数组保存在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();
     }
   }
}

编辑:

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();
}
于 2012-08-14T12:06:57.313 回答