Exemplo n.º 1
0
        public void SaveFileData(FileMetadataDTO fileMetadata)
        {
            FileData fileData = new FileData {
                FileName   = fileMetadata.FileName,
                Location   = fileMetadata.Location,
                UploadedOn = DateTime.Now,
                UpdatedBy  = fileMetadata.UpdatedBy
            };

            Add(fileData);
        }
Exemplo n.º 2
0
        public ActionResult UploadFile()
        {
            try
            {
                var    file        = Request.Form.Files[0];
                string folderName  = "FileUploads";
                string webRootPath = _hostingEnvironment.WebRootPath;
                string newPath     = Path.Combine(webRootPath, folderName);
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }
                if (file.Length > 0)
                {
                    string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    string fullPath = Path.Combine(newPath, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }

                    ClaimsPrincipal currentUser     = this.User;
                    var             currentUserName = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
                    ApplicationUser user            = _userManager.FindByNameAsync(currentUserName).Result;

                    var userName = $"{ user.FirstName }{ user.LastName }";

                    FileMetadataDTO fileDTO = new FileMetadataDTO
                    {
                        FileName  = fileName,
                        Location  = fullPath,
                        UpdatedBy = userName
                    };

                    _homeRepository.SaveFileData(fileDTO);
                    return(Ok("Upload Successful."));
                }

                return(BadRequest());
            }
            catch (System.Exception ex)
            {
                return(BadRequest("Upload Failed: " + ex.Message));
            }
        }