public async Task <IActionResult> UploadToFileSystem(List <IFormFile> files, string description)
 {
     foreach (var file in files)
     {
         var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
         bool basePathExists = System.IO.Directory.Exists(basePath);
         if (!basePathExists)
         {
             Directory.CreateDirectory(basePath);
         }
         var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
         var filePath  = Path.Combine(basePath, file.FileName);
         var extension = Path.GetExtension(file.FileName);
         if (!System.IO.File.Exists(filePath))
         {
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 await file.CopyToAsync(stream);
             }
             var fileModel = new FileOnFileSystemModel
             {
                 CreatedOn   = DateTime.UtcNow,
                 FileType    = file.ContentType,
                 Extension   = extension,
                 Name        = fileName,
                 Description = description,
                 FilePath    = filePath
             };
             context.FilesOnFileSystem.Add(fileModel);
             context.SaveChanges();
         }
     }
     TempData["Message"] = "File successfully uploaded to File System.";
     return(RedirectToAction("Index"));
 }
Example #2
0
        public async Task <IActionResult> UploadToFileSystem(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                //Gets the base Path, i.e, The Current Directory of the application + /Files/. Feel free to change this to your choice.
                var basePath = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");

                //Checks if the base path directory exists, else creates it.
                bool basePathExists = System.IO.Directory.Exists(basePath);

                if (!basePathExists)
                {
                    Directory.CreateDirectory(basePath);
                }

                //Gets the file name without the extension.
                var fileName = Path.GetFileNameWithoutExtension(file.FileName);

                //Combines the base path with the file name.
                var filePath = Path.Combine(basePath, file.FileName);

                //Gets the extension of the file. (*.png, *.mp4, etc)
                var extension = Path.GetExtension(file.FileName);
                if (!System.IO.File.Exists(filePath))
                {
                    //If the file doesnt exist in the generated path, we use a filestream object, and create a new file, and then copy the contents to it.
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    //Create a new FileOnFileSystemModel object with required values.
                    var fileModel = new FileOnFileSystemModel
                    {
                        CreatedOn   = DateTime.UtcNow,
                        FileType    = file.ContentType,
                        Extension   = extension,
                        Name        = fileName,
                        Description = description,
                        FilePath    = filePath
                    };

                    //Inserts this model to the db via the context instance of efcore.
                    context.FilesOnFileSystem.Add(fileModel);
                    context.SaveChanges();
                }
            }

            //Sets a message in the TempData.
            TempData["Message"] = "File successfully uploaded to File System.";

            //Redirects to the Index Action Method.
            return(RedirectToAction("Index"));
        }
Example #3
0
        public async Task <IActionResult> UploadToFileSystem(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                if (file.Length < _fileSizeLimit)
                {
                    //var filePath = Path.Combine(_config["StoredFilesPath"],
                    //    Path.GetRandomFileName());

                    //https://question-it.com/questions/122762/chto-takoe-parametr-_config-storedfilespath-i-gde-on-dolzhen-nahoditsja
                    var  basePath       = Path.Combine(Directory.GetCurrentDirectory(), "\\StoredFilesPath\\");
                    bool basePathExists = System.IO.Directory.Exists(basePath);
                    if (!basePathExists)
                    {
                        Directory.CreateDirectory(basePath);
                    }
                    var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                    var filePath  = Path.Combine(basePath, file.FileName);
                    var extension = Path.GetExtension(file.FileName);
                    var fileSize  = file.Length;
                    if (!System.IO.File.Exists(filePath))
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }
                        var fileModel = new FileOnFileSystemModel
                        {
                            CreatedOn   = DateTime.UtcNow,
                            FileType    = file.ContentType,
                            Extension   = extension,
                            Name        = fileName,
                            FileSize    = fileSize,
                            Description = description,
                            FilePath    = filePath
                        };
                        context.FilesOnFileSystem.Add(fileModel);
                        context.SaveChanges();
                    }
                }
                else
                {
                    TempData["Message"] = "Multipart boundary length limit exceeded..";
                    //throw new InvalidDataException(
                    //    $"Multipart boundary length limit exceeded.");
                }
            }
            TempData["Message"] = "File successfully uploaded to File System.";
            return(RedirectToAction("Index"));
        }
        public async Task UploadToFileSystemAsync(List <IFormFile> files, string description)
        {
            foreach (var file in files)
            {
                var basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\Files\\");
                var basePathExists = Directory.Exists(basePath);

                if (!basePathExists)
                {
                    Directory.CreateDirectory(basePath);
                }

                var filePath = Path.Combine(basePath, file.FileName);

                if (File.Exists(filePath))
                {
                    continue;
                }

                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                var extension = Path.GetExtension(file.FileName);
                var fileModel = new FileOnFileSystemModel
                {
                    CreatedOn   = DateTime.UtcNow,
                    FileType    = file.ContentType,
                    Extension   = extension,
                    Name        = fileName,
                    Description = description,
                    FilePath    = filePath,
                    Size        = file.Length
                };
                await context.FileOnFileSystem.AddAsync(fileModel);

                await context.SaveChangesAsync();
            }
        }
 public async Task <IActionResult> CreateAsync(Burial burial,
                                               IFormFile img_file, string img_description,
                                               IFormFile notes_file, string notes_description,
                                               IFormFile bone_file, string bone_description)
 {
     // Image Upload
     if (img_file != null)
     {
         var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\wwwroot\\Files\\img\\" + burial.BurialID + "\\");
         bool basePathExists = System.IO.Directory.Exists(basePath);
         if (!basePathExists)
         {
             Directory.CreateDirectory(basePath);
         }
         var fileName  = Path.GetFileNameWithoutExtension(img_file.FileName);
         var filePath  = Path.Combine(basePath, img_file.FileName);
         var extension = Path.GetExtension(img_file.FileName);
         if (!System.IO.File.Exists(filePath))
         {
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 await img_file.CopyToAsync(stream);
             }
             var fileModel = new FileOnFileSystemModel
             {
                 CreatedOn   = DateTime.UtcNow,
                 FileType    = img_file.ContentType,
                 Extension   = extension,
                 Name        = fileName,
                 Description = img_description,
                 FilePath    = filePath
             };
             _BurialContext.FileOnFileSystemModels.Add(fileModel);
             _BurialContext.SaveChanges();
             burial.ImgOnSystem = _BurialContext.FileOnFileSystemModels.Where(f => f.FilePath == fileModel.FilePath).FirstOrDefault().Id;
         }
         else
         {
             ViewBag.status = "Photo already esists with this name and file type";
             return(View());
         }
     }
     // Field Notes Upload
     if (notes_file != null)
     {
         var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\wwwroot\\Files\\notes\\" + burial.BurialID + "\\");
         bool basePathExists = System.IO.Directory.Exists(basePath);
         if (!basePathExists)
         {
             Directory.CreateDirectory(basePath);
         }
         var fileName  = Path.GetFileNameWithoutExtension(notes_file.FileName);
         var filePath  = Path.Combine(basePath, notes_file.FileName);
         var extension = Path.GetExtension(notes_file.FileName);
         if (!System.IO.File.Exists(filePath))
         {
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 await notes_file.CopyToAsync(stream);
             }
             var fileModel = new FileOnFileSystemModel
             {
                 CreatedOn   = DateTime.UtcNow,
                 FileType    = notes_file.ContentType,
                 Extension   = extension,
                 Name        = fileName,
                 Description = notes_description,
                 FilePath    = filePath
             };
             _BurialContext.FileOnFileSystemModels.Add(fileModel);
             _BurialContext.SaveChanges();
             burial.NoteBookOnSystem = _BurialContext.FileOnFileSystemModels.Where(f => f.FilePath == fileModel.FilePath).FirstOrDefault().Id;
         }
         else
         {
             ViewBag.status = "Photo already esists with this name and file type";
             return(View());
         }
     }
     // Bone Book Upload
     if (bone_file != null)
     {
         var  basePath       = Path.Combine(Directory.GetCurrentDirectory() + "\\wwwroot\\Files\\bonebooks\\" + burial.BurialID + "\\");
         bool basePathExists = System.IO.Directory.Exists(basePath);
         if (!basePathExists)
         {
             Directory.CreateDirectory(basePath);
         }
         var fileName  = Path.GetFileNameWithoutExtension(bone_file.FileName);
         var filePath  = Path.Combine(basePath, bone_file.FileName);
         var extension = Path.GetExtension(bone_file.FileName);
         if (!System.IO.File.Exists(filePath))
         {
             using (var stream = new FileStream(filePath, FileMode.Create))
             {
                 await bone_file.CopyToAsync(stream);
             }
             var fileModel = new FileOnFileSystemModel
             {
                 CreatedOn   = DateTime.UtcNow,
                 FileType    = bone_file.ContentType,
                 Extension   = extension,
                 Name        = fileName,
                 Description = bone_description,
                 FilePath    = filePath
             };
             _BurialContext.FileOnFileSystemModels.Add(fileModel);
             _BurialContext.SaveChanges();
             burial.BoneBookOnSystem = _BurialContext.FileOnFileSystemModels.Where(f => f.FilePath == fileModel.FilePath).FirstOrDefault().Id;
         }
         else
         {
             ViewBag.status = "Photo already esists with this name and file type";
             return(View());
         }
     }
     _BurialContext.Burials.Add(burial);
     _BurialContext.SaveChanges();
     return(View("Success", burial));
 }