Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ImportService.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.AspNetCore.Http;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class ImportService : IImportService
  5. {
  6. private readonly ILogger<ImportService> _logger;
  7. private readonly ISaveImportedDataService _service;
  8. public ImportService(ILogger<ImportService> logger, ISaveImportedDataService service)
  9. {
  10. _logger = logger;
  11. _service = service;
  12. }
  13. public async Task<List<ApplicantImportDto>> Import(IFormFile fileData)
  14. {
  15. try
  16. {
  17. using (var stream = new MemoryStream())
  18. {
  19. fileData.CopyTo(stream);
  20. var FileData = stream.ToArray();
  21. CopyFileToDirectory(FileData);
  22. }
  23. return await _service.Save();
  24. }
  25. catch (Exception ex)
  26. {
  27. throw new Exception(ex.Message);
  28. }
  29. return new List<ApplicantImportDto>();
  30. }
  31. private async Task CopyFileToDirectory(byte[] FileData)
  32. {
  33. try
  34. {
  35. var content = new System.IO.MemoryStream(FileData);
  36. var path = Path.Combine(Directory.GetCurrentDirectory(), "Files", "s.xlsx");
  37. await CopyStream(content, path);
  38. }
  39. catch (Exception)
  40. {
  41. throw;
  42. }
  43. }
  44. private async Task CopyStream(Stream stream, string downloadPath)
  45. {
  46. using (var fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write))
  47. {
  48. await stream.CopyToAsync(fileStream);
  49. }
  50. }
  51. }
  52. }