예제 #1
0
        IEnumerable <FileDescriptionShort> IFileRepository.AddFileDescriptions(DataAccess.Model.FileResult fileResult)
        {
            List <string> filenames = new List <string>();

            for (int i = 0; i < fileResult.FileNames.Count(); i++)
            {
                int index     = fileResult.FileNames[i].LastIndexOf("\\", StringComparison.Ordinal);
                var shortName = fileResult.FileNames[i].Substring(index + 1);

                var fileDescription = new FileDescription
                {
                    ContentType      = fileResult.ContentTypes[i],
                    FileName         = shortName,
                    CreatedTimestamp = fileResult.CreatedTimestamp,
                    UpdatedTimestamp = fileResult.UpdatedTimestamp,
                    Description      = fileResult.Description
                };

                filenames.Add(fileResult.FileNames[i]);
                _context.FileDescriptions.Add(fileDescription);
            }

            _context.SaveChanges();
            return(GetNewFiles(filenames));
        }
        public async Task <IActionResult> UploadFiles(FileDescriptionShort fileDescriptionShort)
        {
            var names        = new List <string>();
            var contentTypes = new List <string>();

            if (ModelState.IsValid)
            {
                // http://www.mikesdotnetting.com/article/288/asp-net-5-uploading-files-with-asp-net-mvc-6
                // http://dotnetthoughts.net/file-upload-in-asp-net-5-and-mvc-6/
                foreach (var file in fileDescriptionShort.File)
                {
                    if (file.Length > 0)
                    {
                        var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        contentTypes.Add(file.ContentType);

                        names.Add(fileName);

                        // Extension method update RC2 has removed this
                        await file.SaveAsAsync(Path.Combine(_optionsApplicationConfiguration.Value.ServerUploadFolder, fileName));
                    }
                }
            }

            var files = new FileResult
            {
                FileNames        = names,
                ContentTypes     = contentTypes,
                Description      = fileDescriptionShort.Description,
                CreatedTimestamp = DateTime.UtcNow,
                UpdatedTimestamp = DateTime.UtcNow,
            };

            _fileRepository.AddFileDescriptions(files);

            return(RedirectToAction("ViewAllFiles", "FileClient"));
        }