private string SavePostedFile(UploadedFile uploadedFile) { if (!uploadedFile.IsValid) return "File is not valid"; var usersDal = new UsersDal(); var fileExtension = GetExtension(uploadedFile.FileName); var guid = Guid.NewGuid(); var dirPath = MapPath(UploadDirectory + AuthProvider.GetNameWithoutDomain(Page.User.Identity.Name)); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string fileName = Path.Combine(dirPath, guid.ToString() + fileExtension); uploadedFile.SaveAs(fileName, true); //create a Bitmap from the file and add it to the list var bitmap = new Bitmap(fileName); if (bitmap.Height > 128 || bitmap.Width > 128) { return "File size is invalid. Image can not be bigger than 128x128 pixels."; } usersDal.RegisterNewUserAvatar(guid.ToString() + fileExtension, AuthProvider.UserKey(Session)); //clear old Data var emblemKey = usersDal.GetUserEmblem(AuthProvider.UserKey(Session)); var avatarKey = usersDal.GetUserAvatar(AuthProvider.UserKey(Session)); var filePath1 = dirPath + "\\" + (avatarKey ?? string.Empty); var filePath2 = dirPath + "\\" + (emblemKey ?? string.Empty); foreach (var f in Directory.GetFiles(dirPath)) { try { if ((f != filePath1) && (f != filePath2)) File.Delete(f); } catch (Exception) { } } return string.Empty; }
public void DeleteAllTempFiles() { var usersDal = new UsersDal(); var emblemKey = usersDal.GetUserEmblem(AuthProvider.UserKey(Session)); var avatarKey = usersDal.GetUserAvatar(AuthProvider.UserKey(Session)); var dirPath = MapPath(BadgesLogoDirectory); var filePath1 = dirPath + "\\" + (avatarKey ?? string.Empty); var filePath2 = dirPath + "\\" + (emblemKey ?? string.Empty); foreach (var f in Directory.GetFiles(dirPath)) { try { if ((f != filePath1) && (f != filePath2)) File.Delete(f); } catch (Exception) { } } }