我在SQL Server数据库中有一些图像,我想在ASP.NET中检索这些图像。但它给了我一个错误
文件' C:\ Program Files \ Common Files \ Microsoft 共享\ 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();
}
}
答案 0 :(得分:5)
您的问题是image1
和image2
相同。
string image1 = Convert.ToString(DateTime.Now.ToFileTime());
string image2 = Convert.ToString(DateTime.Now.ToFileTime());
考虑使用某些东西来区分两个文件名:
string image1 = Convert.ToString(DateTime.Now.ToFileTime()) + "1";
string image2 = Convert.ToString(DateTime.Now.ToFileTime()) + "2";
您可以想象,创建文件名Convert.ToString(DateTime.Now.ToFileTime())
所涉及的代码执行速度非常快,因此没有足够的时间让DateTime.Now
增加其值。
修改强>
您也可以更改图像的写入目录:
FileStream fs1 = new FileStream(Server.MapPath("~/images/" + image1), FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(Server.MapPath("~/images/" + image2), FileMode.CreateNew, FileAccess.Write);
否则,文件将写入应用程序文件夹(bin
)
然后
Image1.ImageUrl = "~/images/" + image1;
Image2.ImageUrl = "~/images/" + image2;
现在有意义
确保ASP.NET用户在“images”文件夹中具有写入权限。
答案 1 :(得分:0)
此外,如果找到具有重复文件名的文件,则代码设置为抛出错误:
FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.CreateNew, FileAccess.Write);
以下代码允许通过将FileMode.CreateNew
更改为FileMode.Create
来覆盖文件:
FileStream fs1 = new FileStream(image1, FileMode.Create, FileAccess.Write);
FileStream fs2 = new FileStream(image2, FileMode.Create, FileAccess.Write);