protected void UploadClick(object sender, EventArgs e) { if (!SelectedMovieId.HasValue) { lblSelectedMovie.Text = "Please select a movie"; return; } if (!FileUpload1.HasFiles) { return; } var movie = Movies[SelectedMovieId.Value]; AzureContainer azureContainer = new AzureContainer("StorageConnectionString"); azureContainer.SetContainer(GetMovieContainerName(movie)); bool isNewMoviePhotosAvailable = false; var files = FileUpload1.PostedFiles; using (var dataContext = new ProjectManhattanEntities1()) { foreach (var file in files) { var photoURL = azureContainer.StoreFile(file.FileName, file.InputStream); MoviePhoto moviePhoto = dataContext.MoviePhotoes .Where(m => m.MovieId == SelectedMovieId.Value).ToList() .FirstOrDefault(m => string.Equals(Path.GetFileName(m.MoviePhotoURL), Path.GetFileName(photoURL), StringComparison.CurrentCultureIgnoreCase)); if (moviePhoto == null) { moviePhoto = new MoviePhoto() { MovieId = movie.MovieId, MoviePhotoURL = photoURL, }; dataContext.MoviePhotoes.AddObject(moviePhoto); isNewMoviePhotosAvailable = true; } } if (isNewMoviePhotosAvailable) { dataContext.SaveChanges(); RefreshPhotos(); } } }
protected void btnDeleteSelectedFiles_OnClick(object sender, EventArgs e) { if (!SelectedMovieId.HasValue) { lblSelectedMovie.Text = "Please select a movie"; return; } var movie = Movies[SelectedMovieId.Value]; AzureContainer azureContainer = new AzureContainer("StorageConnectionString"); azureContainer.SetContainer(GetMovieContainerName(movie)); var items = CheckBoxList1.Items; using (var dataContext = new ProjectManhattanEntities1()) { foreach (ListItem item in items) { if (item.Selected) { azureContainer.DeleteFile(item.Text); var moviePhoto = dataContext.MoviePhotoes .Where(m => m.MovieId == SelectedMovieId.Value).ToList() .FirstOrDefault(m => string.Equals(Path.GetFileName(m.MoviePhotoURL), item.Text, StringComparison.CurrentCultureIgnoreCase)); dataContext.MoviePhotoes.DeleteObject(moviePhoto); dataContext.SaveChanges(); } } RefreshPhotos(); } }