如何使用C#从ASP.NET中的SQL Server数据库中检索图像?

时间:2022-06-10 21:21:03

I have some images in a SQL Server database and I want to retrieve those images in ASP.NET. But it gives me an error

我在SQL Server数据库中有一些图像,我想在ASP.NET中检索这些图像。但它给了我一个错误

The file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\130065367816821657' already exists.

文件'C:\ Program Files \ Common Files \ Microsoft Shared \ DevServer \ 10.0 \ 130065367816821657'已存在。

Please solve my problem.

请解决我的问题。

protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
        cn.Open();
        SqlCommand cm = new SqlCommand("select * from imageCollection where img_id='" + DropDownList1.SelectedItem.ToString() + "'", cn);
        SqlDataAdapter da = new SqlDataAdapter(cm);
        SqlDataReader dr = cm.ExecuteReader();

        try
        {
            if (dr.Read())
            {

                string image1 = Convert.ToString(DateTime.Now.ToFileTime());
                string image2 = Convert.ToString(DateTime.Now.ToFileTime());
                FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
                FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
                byte[] bimage1 = (byte[])dr["passport_photo"];
                byte[] bimage2 = (byte[])dr["sign_photo"];
                fs1.Write(bimage1, 0, bimage1.Length - 1);
                fs2.Write(bimage2, 0, bimage2.Length - 1);
                fs1.Flush();
                fs2.Flush();
                Image1.ImageUrl = "~/images"+bimage1.ToString();
                Image2.ImageUrl = "~/images"+bimage2.ToString();
            }
            dr.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }

I have uploaded images from "C:\Program Files\Common Files\microsoft shared\DevServer\10.0" and the front end code is as of following:

我上传了“C:\ Program Files \ Common Files \ microsoft shared \ DevServer \ 10.0”中的图片,前端代码如下:

 protected void Button1_Click(object sender, EventArgs e)
{
    string image1 = FileUpload1.FileName;
    string image2 = FileUpload2.FileName;
    FileStream fs1 = new FileStream(image1, FileMode.Open, FileAccess.Read);
    FileStream fs2 = new FileStream(image2, FileMode.Open, FileAccess.Read);
    byte[] bimage1 = new byte[fs1.Length];
    byte[] bimage2 = new byte[fs2.Length];
    fs1.Read(bimage1, 0, Convert.ToInt32(fs1.Length));
    fs2.Read(bimage2, 0, Convert.ToInt32(fs2.Length));
    fs1.Close();
    fs2.Close();
    cn.Open();
    SqlParameter sp = new SqlParameter();
    sp.SqlDbType = SqlDbType.Image;
    sp.ParameterName = "@passport_photo";
    sp.ParameterName = "@sign_photo";
    sp.Value = bimage1;
    sp.Value = bimage2;
    SqlCommand cm = new SqlCommand("INSERT INTO imageCollection values(@img_id," + "@passport_photo,"+"@sign_photo)", cn);
    cm.Parameters.AddWithValue("@img_id",TextBox1.Text);
    cm.Parameters.AddWithValue("@passport_photo",sp.Value=bimage1);
    cm.Parameters.AddWithValue("@sign_photo",sp.Value=bimage2);
    cm.ExecuteNonQuery();
    cm.Dispose();
    cn.Dispose();
    cn.Close();
}

}

}

2 个解决方案

#1


5  

Your problem is that image1 and image2 are equal.

你的问题是image1和image2是相同的。

string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());

Consider using something to differentiate both filenames:

考虑使用某些东西来区分两个文件名:

string image1 = Convert.ToString(DateTime.Now.ToFileTime()) + "1";
string image2 = Convert.ToString(DateTime.Now.ToFileTime()) + "2";

As you can imagine, the code involved in create your filename Convert.ToString(DateTime.Now.ToFileTime()) executes really fast, so there is not time enough to let DateTime.Now increases its value.

您可以想象,创建文件名所涉及的代码Convert.ToString(DateTime.Now.ToFileTime())执行速度非常快,因此没有足够的时间让DateTime.Now增加其值。

EDIT

编辑

You may also change the directory where the images are written:

您还可以更改写入图像的目录:

 FileStream fs1 = new FileStream(Server.MapPath("~/images/" + image1), FileMode.CreateNew, FileAccess.Write);
 FileStream fs2 = new FileStream(Server.MapPath("~/images/" + image2), FileMode.CreateNew, FileAccess.Write);

Otherwise, the files are written in the application folder (bin)

否则,文件将写入应用程序文件夹(bin)

And then

接着

Image1.ImageUrl = "~/images/" + image1;
Image2.ImageUrl = "~/images/" + image2;

Now have sense

现在有意义

Ensure that the ASP.NET user has write permissions in the "images" folder.

确保ASP.NET用户在“images”文件夹中具有写入权限。

#2


0  

Also, your code is set to throw an error if a file with a duplicate filename is found:

此外,如果找到具有重复文件名的文件,则代码设置为抛出错误:

FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);

The following code will allow a file to be overwritten, by changing FileMode.CreateNew to FileMode.Create:

通过将FileMode.CreateNew更改为FileMode.Create,以下代码将允许覆盖文件:

FileStream fs1 = new FileStream(image1, FileMode.Create, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.Create, FileAccess.Write);

#1


5  

Your problem is that image1 and image2 are equal.

你的问题是image1和image2是相同的。

string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());

Consider using something to differentiate both filenames:

考虑使用某些东西来区分两个文件名:

string image1 = Convert.ToString(DateTime.Now.ToFileTime()) + "1";
string image2 = Convert.ToString(DateTime.Now.ToFileTime()) + "2";

As you can imagine, the code involved in create your filename Convert.ToString(DateTime.Now.ToFileTime()) executes really fast, so there is not time enough to let DateTime.Now increases its value.

您可以想象,创建文件名所涉及的代码Convert.ToString(DateTime.Now.ToFileTime())执行速度非常快,因此没有足够的时间让DateTime.Now增加其值。

EDIT

编辑

You may also change the directory where the images are written:

您还可以更改写入图像的目录:

 FileStream fs1 = new FileStream(Server.MapPath("~/images/" + image1), FileMode.CreateNew, FileAccess.Write);
 FileStream fs2 = new FileStream(Server.MapPath("~/images/" + image2), FileMode.CreateNew, FileAccess.Write);

Otherwise, the files are written in the application folder (bin)

否则,文件将写入应用程序文件夹(bin)

And then

接着

Image1.ImageUrl = "~/images/" + image1;
Image2.ImageUrl = "~/images/" + image2;

Now have sense

现在有意义

Ensure that the ASP.NET user has write permissions in the "images" folder.

确保ASP.NET用户在“images”文件夹中具有写入权限。

#2


0  

Also, your code is set to throw an error if a file with a duplicate filename is found:

此外,如果找到具有重复文件名的文件,则代码设置为抛出错误:

FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);

The following code will allow a file to be overwritten, by changing FileMode.CreateNew to FileMode.Create:

通过将FileMode.CreateNew更改为FileMode.Create,以下代码将允许覆盖文件:

FileStream fs1 = new FileStream(image1, FileMode.Create, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.Create, FileAccess.Write);