我在 SQL Server 数据库中有一些图像,我想在 ASP.NET 中检索这些图像。但这给了我一个错误
文件“C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\130065367816821657”已经存在。
请解决我的问题。
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;
}
我从“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();
}
}