public async Task <IActionResult> UploadToDatabase(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var fileModel = new FileOnDatabaseModel
                {
                    CreatedOn   = DateTime.UtcNow,
                    FileType    = file.ContentType,
                    Extension   = extension,
                    Name        = fileName,
                    Description = description
                };
                using (var dataStream = new MemoryStream())
                {
                    await file.CopyToAsync(dataStream);

                    fileModel.Data = dataStream.ToArray();
                }
                context.FilesOnDatabase.Add(fileModel);
                context.SaveChanges();
            }
            TempData["Message"] = "File successfully uploaded to Database";
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> UploadToDatabase(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var fileModel = new FileOnDatabaseModel
                {
                    CreatedOn   = DateTime.UtcNow,
                    FileType    = file.ContentType,
                    Extension   = extension,
                    Name        = fileName,
                    Description = description,
                    UserId      = _userManager.GetUserId(User)
                };
                using (var dataStream = new MemoryStream())
                {
                    await file.CopyToAsync(dataStream);

                    fileModel.Data = dataStream.ToArray();
                }
                _context.FilesOnDatabase.Add(fileModel);
                _context.SaveChanges();
            }
            TempData["success"] = "Файл успешно сохранен в базу данных";
            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public async Task <IActionResult> UploadToDatabase(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                //Gets the file name without the extension.
                var fileName = Path.GetFileNameWithoutExtension(file.FileName);

                //Gets the extension of the file. (*.png, *.mp4, etc)
                var extension = Path.GetExtension(file.FileName);
                var fileModel = new FileOnDatabaseModel
                {
                    CreatedOn   = DateTime.UtcNow,
                    FileType    = file.ContentType,
                    Extension   = extension,
                    Name        = fileName,
                    Description = description
                };
                //Creates a new MemoryStream object , convert file to memory object and appends ito our model’s object.
                using (var dataStream = new MemoryStream())
                {
                    await file.CopyToAsync(dataStream);

                    fileModel.Data = dataStream.ToArray();
                }
                context.FilesOnDatabase.Add(fileModel);
                context.SaveChanges();
            }
            TempData["Message"] = "File successfully uploaded to Database";
            return(RedirectToAction("Index"));
        }
Esempio n. 4
0
        public async Task UploadToDatabaseAsync(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var fileModel = new FileOnDatabaseModel
                {
                    CreatedOn   = DateTime.UtcNow,
                    FileType    = file.ContentType,
                    Extension   = extension,
                    Name        = fileName,
                    Description = description,
                    Size        = file.Length
                };
                using (var dataStream = new MemoryStream())
                {
                    await file.CopyToAsync(dataStream);

                    fileModel.Data = dataStream.ToArray();
                }
                await context.FileOnDatabase.AddAsync(fileModel);

                await context.SaveChangesAsync();
            }
        }