我有一个 ASP.NET (4.5) 网站。
下面的这个按钮命令将employeename、employeelastname、employeephoto 插入到images 文件夹,将imagepath 插入到db。
我希望图像路径自动成为唯一名称+扩展名;
string filepath = //"uniquename" + ext;
因此,EmployeeID 是唯一的,因此不会出现混淆或重复的照片。
我怎样才能做到这一点?
谢谢你。
protected void Button1_Click(object sender, EventArgs e) //Insert
{
if (FileUploadControl.HasFile)
{
string ext = Path.GetExtension(FileUploadControl.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg" || ext == ".gif")
{
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Employees (EmployeeFirstName, EmployeeLastName, EmployeePhotoPath) VALUES (@item1,@item2,@img)", cnn);
cmd.Parameters.AddWithValue("@item1", TextBox1.Text);
cmd.Parameters.AddWithValue("@item2", TextBox2.Text);
string filepath = //"uniquename" + ext;
FileUploadControl.SaveAs(Server.MapPath("Images/") + filepath);
cmd.Parameters.AddWithValue("@img", filepath);
cmd.ExecuteNonQuery();
cnn.Close();
Label3.Text = "successfully inserted";
}
catch (Exception ex)
{
Label3.Text = ex.Message;
}
temizle();
}
else
{
Label3.Text = "selected file is not a photo";
}
}
else
{
Label3.Text = "please upload employee photo";
}
}
}