如何在保存文件之前知道上传文件的扩展名

时间:2016-12-19 10:45:24

标签: c# asp.net asp.net-mvc

     HttpPostedFile file = context.Request.Files[j];
     string fileName = file.FileName;
     string fileExtension = System.IO.Path.GetExtension(filepath + file.FileName);
     if (!string.IsNullOrEmpty(fileName)
     {
        string pathToSave_100 = HttpContext.Current.Server.MapPath(filepath) + fileName + fileExtension;
        if (File.Exists(pathToSave_100))
        {
           File.Delete(pathToSave_100);
           file.SaveAs(pathToSave_100);
        }
        else
        {
           file.SaveAs(pathToSave_100);
        }
     }

1 个答案:

答案 0 :(得分:0)

你可以这样做:

string strFileExtension = Path.GetExtension(file.FileName);