如何在存储之前更改文件名

Ima*_*man 0 c# asp.net-mvc

在我的视图中,我允许用户上传简历和可选的求职信.但是为避免重复,我应该能够更改文件名.下面是我控制器中的代码段.请告知我如何在存储之前更改文件名.

foreach (string upload in Request.Files)
{
    if (Request.Files[upload].ContentLength == 0) continue;
    string pathToSave = Server.MapPath("~/Documents/");
    string filename = Path.GetFileName(Request.Files[upload].FileName);
    Request.Files[upload].SaveAs(Path.Combine(pathToSave, filename));
}
Run Code Online (Sandbox Code Playgroud)

Olu*_*emi 5

您可以使用DateTime值使其唯一

 Request.Files[upload].SaveAs(Path.Combine(pathToSave, DateTime.Now.ToString("yyyy_MM_dd_mm_ss") + "_" + filename));
Run Code Online (Sandbox Code Playgroud)

  • 这就是我的工作.@olu打败了我:) (2认同)