如何更改fileupload名称

时间:2010-04-12 11:11:05

标签: asp.net

即时使用以下代码上传

protected void UploadButton_Click(object sender, EventArgs e)
{
  if(FileUploadControl.HasFile)
  {
    try
    {
        string filename = Path.GetFileName(FileUploadControl.FileName);
        FileUploadControl.SaveAs(Server.MapPath("~upload/") + filename);
        StatusLabel.Text = "Upload status: File uploaded!";
    }
    catch(Exception ex)
    {
        StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
    }
  }
}

我想更改上传文件名...我必须为每个上传的文件分配文件名如何?

2 个答案:

答案 0 :(得分:5)

此行获取上传文件的名称:

string filename = Path.GetFileName(FileUploadControl.FileName);

该行告诉服务器将文件保存为:

 FileUploadControl.SaveAs(Server.MapPath("~/") + filename);

在保存之前,只需将filename的值更改为其他内容即可。

答案 1 :(得分:2)

有一种方法可以更改我们下载到“~Server / Userfolder”的文件的名称(作为示例)..

我真的在考虑在.SaveAs命令之前重命名文件,这将是:

if (Request.Files != null)
{
    var getmyfile = Request.Files["some_photo"];
    try
    {
        if (getmyfile.FileName != "" && getmyfile.FileName != null && getmyfile.ContentLength > 0)
        {
            ///Create Useruploads path.
            var path = Server.MapPath("~/Useruploads" + "\\");

            ///Get file info that we gonna upload to server..
            FileInfo TheFile = new FileInfo(path + Path.GetFileName(getmyfile.FileName));
            ///Note: i used Path.GetFileName(getmyfile.FileName) code cause getmyfile.FileName gives full path of that file..
            if (TheFile.Exists)
            {
                ///Seperate file name and extention..
                var fname = Path.GetFileNameWithoutExtension(TheFile.Name);
                var ext = TheFile.Extension;
                var path2 = path + fname;

                ///As long as we got file names, same as in server..
                while(new FileInfo(path2+ext).Exists)
                {
                ///Add "-img" to its name..
                    path2 = path2 + "-img";
                }

                FileInfo ccc = new FileInfo(path2 + ext);
                string f_art = ccc.Name.Replace(" ", "");///Clean space.. (Optional)

                getmyfile.SaveAs(f_art);
                somedb.some_photo = Path.GetFileName(f_art); ///Its not fart its file art lol!

            }
            else  ///if file is not exist in our server..
            {
                var path3 = (path + Path.GetFileName(getmyfile.FileName));

                FileInfo ccc = new FileInfo(path3);
                string f_art = ccc.Name.Replace(" ", "");

                getmyfile.SaveAs(f_art);
                some.some_photo = Path.GetFileName(f_art);
            }
        }
    }
    catch(FileNotFoundException ex)
    {
    form.Set("lblStatus",ex.Message);
    }
    catch (Exception ex)
    {
    form.Set("lblStatus", ex.Message);
    }

    db.somedbs.InsertOnSubmit(somedb);
    db.SubmitChanges();
    return RedirectToAction("Index");
}

(可选)您也可以使用:

var f_art = System.Text.RegularExpressions.Regex.Replace(Path.GetFileNameWithoutExtension(TheFile.Name), "[^a-zA-Z]", "");

而不是使用.Replace(“”,“”);但在此它只是清理间隔区域,如果你在那里使用代码,它将清除除A到Z之外的所有字符,a到z字符,这意味着没有数字将应用于该名称,也不是空格或其他不在A和A之间的字符ž..