简体   繁体   English

使图像路径唯一

[英]make imagepath unique

I have an ASP.NET (4.5) website. 我有一个ASP.NET(4.5)网站。

This button command below that inserts employeename, employeelastname, employeephoto to images folder and imagepath to db. 下面的此按钮命令可将employeename,employeelastname,employeephoto插入images文件夹,并将imagepath插入db。

I want that the imagepath to be automatically uniquename + extension; 我希望图像路径自动为唯一名称+扩展名;

string filepath = //"uniquename" + ext;

so that, EmployeeID is unique so there is no confusion or duplicate photos. 因此,EmployeeID是唯一的,因此不会造成混淆或重复的照片。

How can I achieve that? 我该如何实现?

Thank you. 谢谢。

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";
            }
        }
   }

If all you need is for the file names to be unique and you don't necessarily care about their names, a simple solution would be to use a guid for the file name: 如果您只需要文件名唯一,而不必关心它们的名称,那么一个简单的解决方案是为文件名使用guid:

string filepath = Guid.NewGuid().ToString() + ext;

It would probably also be a good idea to add the employee name to the filename so it will be easier to identify the files just by looking at their names: 将雇员姓名添加到文件名中可能也是一个好主意,这样仅通过查看文件名就可以更容易识别它们:

string filepath = string.Format("{0}-{1}-{2}{3}",
                                TextBox1.Text,
                                TextBox2.Text,
                                Guid.NewGuid().ToString(),
                                ext);

Another option, if you don't want to do the INSERT and SCOPE_IDENTITY route is to use a GUID . 如果您不想执行INSERTSCOPE_IDENTITY路由,另一种选择是使用GUID

Trying to build your own unique IDs (like through a combination of names + times or anything else you can think up) is more likely to produce a duplicate than a GUID. 尝试构建自己的唯一ID(例如通过名称+时间或您可以想到的其他方式的组合)比GUID更有可能产生重复。

string filepath = Guid.NewGuid().ToString() + ext;

I will start creating a stored procedure to delegate the job to add the record in the database figuring out the name of the file. 我将开始创建一个存储过程来委派该作业,以将记录添加到数据库中,以确定文件名。 The stored procedure returns the new ID and you can catch it outside as a scalar value 存储过程返回新的ID,您可以将其作为标量值捕获到外部

CREATE PROCEDURE EmployeeAdd
(
   @firstN nvarchar(50),
   @lastN nvarchar(50),
   @ext nvarchar(10)
)
as
BEGIN
     DECLARE @newID int
     INSERT INTO Employees (EmployeeFirstName, EmployeeLastName, EmployeePhotoPath) 
                 VALUES (@firstN, @lastN, '')
     SET @newID = SCOPE_IDENTITY()
     DECLARE @newPath nvarchar(30)
     SET @newPath = CONVERT(nvarchar(10), @newID) + '.' + @ext
     UPDATE Employees SET EmployeePhotoPath = @newPath WHERE EmployeeID = @newID
     SELECT @newID
END

......    
SqlCommand cmd = new SqlCommand("EmployeeAdd", cnn);
cmd.Parameters.AddWithValue("@firstN", TextBox1.Text);
cmd.Parameters.AddWithValue("@lastN", TextBox2.Text);
cmd.Parameters.AddWithValue("@ext", ext);
int result = Convert.ToInt32(cmd.ExecuteScalar());
FileUploadControl.SaveAs(Server.MapPath("Images/") + string.Format("{0}.{1}", result, ext));
....

Can't test all just now, but I think that the idea is clear. 暂时无法测试所有内容,但我认为这个想法很明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM