videoController.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using Aliyun.Acs.Core.Http;
  4. using Aliyun.Acs.Core.Utils;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.StaticFiles;
  9. namespace GxPress.Api.AppControllers
  10. {
  11. /// <summary>
  12. /// 访问
  13. /// </summary>
  14. [Route("/api/app/video")]
  15. [ApiController]
  16. [Authorize]
  17. public class VideoController : ControllerBase
  18. {
  19. private readonly IWebHostEnvironment _environment;
  20. public VideoController(IWebHostEnvironment environment)
  21. {
  22. _environment = environment;
  23. }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. /// <returns></returns>
  28. [HttpGet]
  29. [AllowAnonymous]
  30. public IActionResult GetVideo()
  31. {
  32. try
  33. {
  34. // HttpResponse response = new HttpResponse();
  35. var dd = Common.Tools.StringUtils.GetWebRootPath(_environment.WebRootPath);
  36. var fis = new FileStream(dd + "/cache/20200428/music.mp4", FileMode.Open);
  37. var size = fis.Length; // 得到文件大小
  38. var data = new byte[size];
  39. fis.Write(data, 0, data.Length);
  40. var filepath = dd + "/cache/20200428/music.mp4";
  41. var provider = new FileExtensionContentTypeProvider();
  42. FileInfo fileInfo = new FileInfo(filepath);
  43. var ext = fileInfo.Extension;
  44. new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype);
  45. var result = File(data, contenttype ?? "application/octet-stream", fileInfo.Name);
  46. fis.Close();
  47. return result;
  48. }
  49. catch (FileNotFoundException e)
  50. {
  51. throw new Common.Exceptions.BusinessException(e.Message);
  52. }
  53. catch (IOException e)
  54. {
  55. throw new Common.Exceptions.BusinessException(e.Message);
  56. }
  57. }
  58. }
  59. }