FileProcesTaskController.cs 2.1 KB

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