BistroDriveWebApp.Controllers.ProfileController.SaveFile C# (CSharp) 메소드

SaveFile() 개인적인 메소드

Сохроняет файл с HttpPost потока
private SaveFile ( System.Web.HttpPostedFileBase file, string folder, string old_file = null ) : string
file System.Web.HttpPostedFileBase HttpPost поток
folder string Папка сохранения
old_file string Имя старого файла, если необходимо его удалить
리턴 string
        private string SaveFile(HttpPostedFileBase file, string folder, string old_file = null)
        {
            int typeBegin = file.FileName.LastIndexOf('.');
            string fileType = file.FileName.Substring(typeBegin, file.FileName.Length - typeBegin);
            //генерируем уникальное имя
            var fileName = Security.GetMd5(User.Identity.GetUserId() + DateTime.Now.ToFileTimeUtc()) + fileType;
            var path = Path.Combine(Server.MapPath(folder), fileName);
            file.SaveAs(path);
            if (old_file != null && old_file != "/Content/images/default-dish.png")
            {
                path = Server.MapPath(old_file);
                FileInfo fi = new FileInfo(path);
                try
                {
                    fi.Delete();
                }
                catch { }
            }
            return fileName;
        }