如何在C#中使用HttpPostedFileBase连接字符串?

如何在C#中使用HttpPostedFileBase连接字符串?,c#,asp.net-mvc,C#,Asp.net Mvc,我在internet解决方案中找不到如何使用HttpPostedFileBase在文件中连接或附加额外字符串 例如,我使用HttpPostedFileBase获取此图像文件: hello.jpg 我想连接一个字符串: hello00001.jpg 这将帮助我防止使用ASP.NET MVC在服务器中上载同名图像 这是我的代码,非常简单: HttpPostedFileBase archivo = Request.Files["Image"]; if (archivo != n

我在internet解决方案中找不到如何使用HttpPostedFileBase在文件中连接或附加额外字符串

例如,我使用HttpPostedFileBase获取此图像文件:

hello.jpg
我想连接一个字符串:

hello00001.jpg
这将帮助我防止使用ASP.NET MVC在服务器中上载同名图像

这是我的代码,非常简单:

HttpPostedFileBase archivo = Request.Files["Image"];
if (archivo != null && archivo.ContentLength > 0)
   {
      var path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(archivo.FileName));
       archivo.SaveAs(path);

    }
我试过这个:

var path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(archivo.FileName) + "00001");
archivo.SaveAs(path + "00001");
但给我的是:

hello.jpg00001
hello.jpg00001
       
我也试过:

var path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(archivo.FileName) + "00001");
archivo.SaveAs(path + "00001");
也给了我这个,

hello.jpg00001
hello.jpg00001
       
我的问题看起来很简单,但我不知道如何解决。

你可以试试这个

fileName = System.IO.Path.GetFileNameWithoutExtension(file.FileName); // hello
fileName+="00001";
fileExtension = System.IO.Path.GetExtension(file.FileName); // jpg

string path = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), $"{fileName}{fileExtension}"); // ~/Uploads/hello00001.jpg


file.SaveAs(path);

有Path.GetFileNameWithoutExtension和Path.GetExtension