You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Karsha_Site.Common
  10. {
  11. public class UploadFiles
  12. {
  13. private readonly IHostingEnvironment _environment;
  14. public UploadFiles(IHostingEnvironment hostingEnvironment)
  15. {
  16. _environment = hostingEnvironment;
  17. }
  18. public UploadDto UploadFile(IFormFile file, string folder)
  19. {
  20. if (file != null)
  21. {
  22. var uploadsRootFolder = Path.Combine(_environment.WebRootPath, folder);
  23. if (!Directory.Exists(uploadsRootFolder))
  24. {
  25. Directory.CreateDirectory(uploadsRootFolder);
  26. }
  27. if (file == null || file.Length == 0)
  28. {
  29. return new UploadDto()
  30. {
  31. Status = false,
  32. FileNameAddress = "",
  33. };
  34. }
  35. string fileName = DateTime.Now.Ticks.ToString() + file.FileName;
  36. var filePath = Path.Combine(uploadsRootFolder, fileName);
  37. using (var fileStream = new FileStream(filePath, FileMode.Create))
  38. {
  39. file.CopyTo(fileStream);
  40. }
  41. return new UploadDto()
  42. {
  43. FileNameAddress = folder + fileName,
  44. Status = true,
  45. };
  46. }
  47. return null;
  48. }
  49. public class UploadDto
  50. {
  51. public long Id { get; set; }
  52. public bool Status { get; set; }
  53. public string FileNameAddress { get; set; }
  54. }
  55. }
  56. }