Ejemplo n.º 1
0
        //[HttpPost]
        public async Task <IActionResult> AddPhoto(String name, AlbumViewModel model)
        {
            var newFileName = string.Empty;

            if (HttpContext.Request.Form.Files != null)
            {
                var user = await _userManager.GetUserAsync(HttpContext.User);

                var modelUser = _repository.GetUser(user.Id);

                var    fileName = string.Empty;
                string PathDB   = string.Empty;

                var files = HttpContext.Request.Form.Files;

                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        //Getting FileName
                        fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Replace('"', ' ').Trim();

                        //Assigning Unique Filename (Guid)
                        var myUniqueFileName = Convert.ToString(Guid.NewGuid());

                        //Getting file Extension
                        var FileExtension = Path.GetExtension(fileName);

                        // concating  FileName + FileExtension
                        newFileName = myUniqueFileName + FileExtension;

                        // Combines two strings into a path.
                        fileName = Path.Combine(_environment.WebRootPath, "uploads") + $@"\{newFileName}";
                        string thumbnail = Path.Combine(_environment.WebRootPath, "uploads") + $@"\thumbs" +
                                           $@"\{newFileName}";
                        // if you want to store path of folder in database
                        PathDB = "~/uploads/" + newFileName;
                        string thumbnailDB = "~/uploads/thumbs/" + newFileName;
                        using (FileStream fs = System.IO.File.Create(fileName))
                        {
                            file.CopyTo(fs);
                            fs.Flush();
                        }

                        CreateThumbnail(150, fileName, thumbnail);
                        await _repository.AddPhotoToAlbumAsync(model.Id, user.Id, PathDB, thumbnailDB);
                    }
                }
                return(RedirectToAction("ShowAlbumPhotos", new RouteValueDictionary(
                                            new { controller = "Album", action = "ShowAlbumPhotos", id = model.Id })));//popraviti
            }
            return(View("AddPhoto", model));
        }