Guest User

Guest User

  • Tech Writer
  • 611
  • 116.7k

upload multiple images in .net core

Aug 5 2020 11:58 PM
Upload Multiple Images And Store each image path in different column in c#.
 
1 Throwing NULL NULL NULL throwing_white.png throwing_black.png for e.g i have first select throwing_white and second select throwing_black.png . How to store different column a image path
2 Catching NULL NULL NULL catching_white.png catching_black.png
3 Running NULL NULL NULL running_white.png running_black.png
4 Hitting NULL NULL NULL hitting_white.png hitting_black.png
5 Fielding NULL NULL NULL fielding_white.png fielding_black.png
6 Soft Tissue Mobility 2020-07-20 17:30:44.220 1 NULL soft_tissue_white.png soft_tissue_black.png
 
it's me upload and add code
  1. public async Task<IActionResult> AddCategories(CategoriesViewModel obj_Parameter, List<IFormFile> CategoriesImages)  
  2. {  
  3. CategoryMaster obj_category = new CategoryMaster();  
  4. try  
  5. {  
  6. string filePath = "";  
  7. if (CategoriesImages != null && CategoriesImages.Count > 0)  
  8. {  
  9. return Content("File(s) not selected");  
  10. }  
  11. else  
  12. {  
  13. foreach (IFormFile image in CategoriesImages)  
  14. {  
  15. var wwwrootPath = webHost.ContentRootPath + "\\wwwroot\\CategoryImage\\";  
  16. string thisFileName = Path.Combine(image.FileName);  
  17. if (image?.Length > 0)  
  18. {  
  19. filePath = Path.Combine(wwwrootPath, image.FileName);  
  20. using FileStream fileStream = new FileStream(filePath, FileMode.Create);  
  21. await image.CopyToAsync(fileStream);  
  22. }  
  23. obj_category.CategoryWhiteImage = thisFileName;  
  24. obj_category.CategoryBlackImage = "";  
  25. obj_category.IsActive = true;  
  26. obj_category.CategoryName = obj_Parameter.CategoryName;  
  27. obj_category.UpdatedDate = DateTime.UtcNow;  
  28. }  
  29. await _context.CategoryMaster.AddAsync(obj_category);  
  30. await _context.SaveChangesAsync();  
  31. }  
  32. }  
  33. catch (Exception ex)  
  34. {  
it's me view code.
  1. @using (Html.BeginForm("AddCategories""CategoriesManagement", FormMethod.Post, new { enctype = "multipart/form-data" }))  
  2. {  
  3. <div class="modal-content">  
  4. <div class="modal-header">  
  5. <h5 class="modal-title has-icon ms-icon-round ">Add New Category</h5>  
  6. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>  
  7. </div>  
  8. <div class="modal-body">  
  9. <form action="/action_page.php">  
  10. <div class="ms-form-group">  
  11. <label for="img">Select image:</label>   
  12. <input type="file" id="CategoriesImage" name="CategoriesImages" accept="image/*" multiple="multiple"/>  
  13. </div>  
  14. <div class="ms-form-group has-icon">  
  15. <label for="choosecategory">Enter Category</label>  
  16. @Html.TextBoxFor(x => x.CategoryName, new { @type = "text", @id = "ImageName", @class = "form-control", @placeholder = "Enter Name" })  
  17. @*<input type="text" class="form-control" placeholder="Enter your Category">*@  
  18. </div>  
  19. </form>  
  20. </div>  
  21. <div class="modal-footer">  
  22. <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>  
  23. <button type="submit" class="btn btn-info shadow-none" id="SubmitCategories">Submit</button>  
  24. </div>  
  25. </div>  
  26. }  

Answers (1)