FileProcesTaskController.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.StaticFiles;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace wispro.sp.api.Controllers
  11. {
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. [Authorize]
  15. public class FileProcesTaskController : ControllerBase
  16. {
  17. Services.IFileTaskService fileTaskService;
  18. public FileProcesTaskController(Services.IFileTaskService _fileTaskService)
  19. {
  20. fileTaskService = _fileTaskService;
  21. }
  22. public share.FileProcessTask Get(string Id)
  23. {
  24. return fileTaskService.Get(Id);
  25. }
  26. [HttpGet, DisableRequestSizeLimit]
  27. public async Task<IActionResult> Download(string Id)
  28. {
  29. var filename = fileTaskService.Get(Id);
  30. if (filename != null)
  31. {
  32. var attachfileSavePath = utility.ConfigHelper.GetSectionValue("AttachFileSavePath");
  33. var filePath = filename.FilePath;
  34. var context = HttpContext;
  35. if (System.IO.File.Exists(filePath))
  36. {
  37. var memory = new MemoryStream();
  38. await using (var stream = new FileStream(filePath, FileMode.Open))
  39. {
  40. await stream.CopyToAsync(memory);
  41. }
  42. memory.Position = 0;
  43. return File(memory, GetContentType(filename.FilePath), filename.FileName);
  44. }
  45. else
  46. return NotFound();
  47. }
  48. else
  49. {
  50. return NotFound();
  51. }
  52. }
  53. private string GetContentType(string path)
  54. {
  55. var provider = new FileExtensionContentTypeProvider();
  56. string contentType;
  57. if (!provider.TryGetContentType(path, out contentType))
  58. {
  59. contentType = "application/octet-stream";
  60. }
  61. return contentType;
  62. }
  63. }
  64. }