我必须从上传文件更改文件名,并且更改的文件名需要存储在我的文件夹中,例如(上传文件:employee.xlsx)名称更改为(employee + datetimenow).xlsx。下面我给出了我的web api编码
代码
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filename = postedFile.FileName;
var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);
postedFile.SaveAs(filePath);
}
return Request.CreateResponse("Uploaded Successfully!");
}
return Request.CreateResponse("Failed");
}
答案 0 :(得分:2)
这可能会为你做到这一点
var filename = postedFile.FileName;
var FileNameOnly = Path.GetFileNameWithoutExtension(fileName);
Var fileExt = Path.GetExtension(fileName);
var ModFileName = FileNameOnly + DateTime.Now + fileExt;
var filePath = HttpContext.Current.Server.MapPath("~/" + ModFileName);
postedFile.SaveAs(filePath);
答案 1 :(得分:1)
修改循环,如下所示:
'dws_Support.php'