Exemple #1
0
        public async Task <IActionResult> OnPostUpload(string name, ICollection <IFormFile> files)
        {
            var album = _ac.Albums.FirstOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

            foreach (var file in files.Where(f => _ac.IsImageFile(f.FileName)))
            {
                string path = Path.Combine(_environment.WebRootPath, "albums", album.Name, Path.GetFileName(file.FileName));

                if (System.IO.File.Exists(path))
                {
                    path = Path.ChangeExtension(path, file.GetHashCode() + Path.GetExtension(path));
                }

                using (var imageStream = file.OpenReadStream())
                    using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
                    {
                        _processor.CreateThumbnails(imageStream, path);
                        await file.CopyToAsync(fileStream);
                    }

                var photo = new Photo(album, new FileInfo(path));
                album.Photos.Insert(0, photo);
            }

            album.Sort();

            return(new RedirectResult($"~/album/{WebUtility.UrlEncode(name).Replace('+', ' ')}/"));
        }