I use these following codes for using guid for file names in asp.net uploading forms.

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        Guid filename = new Guid();
        FileUpload1.SaveAs(Server.MapPath("~/upload/") + filename.ToString());
    }
}

But it only shows one file name and it's 00000000-0000-0000-0000-000000000000.

how can I solve this problem.

Thank in advance

有帮助吗?

解决方案

change you code to this:

if (FileUpload1.HasFile == true)
{
    string filename = Guid.NewGuid().ToString(); // you used one extra new before Guid.NewGuid().ToString();
    FileUpload1.SaveAs(Server.MapPath("~/upload/") + filename);
}

其他提示

Guid use factory pattern approach change

Guid filename = new Guid();

to

string filename = Guid.NewGuid().ToString();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top