FileController.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.StaticFiles;
  7. using Procurement.Enums;
  8. using Volo.Abp.AspNetCore.Mvc;
  9. using Procurement.ViewModel;
  10. using Procurement.Helpers;
  11. using Microsoft.AspNetCore.Hosting;
  12. namespace Procurement.Controllers
  13. {
  14. [Produces("application/json")]
  15. [Route("api/[controller]")]
  16. public class FileController : AbpController
  17. {
  18. private readonly IWebHostEnvironment _hostEnvironment;
  19. public FileController(IWebHostEnvironment hostEnvironment)
  20. {
  21. _hostEnvironment = hostEnvironment;
  22. }
  23. [Route("version")]
  24. [HttpGet]
  25. public async Task<IActionResult> Get(string app = "sfm")
  26. {
  27. ResultCode code = ResultCode.Fail, subCode = ResultCode.Fail;
  28. string subMsg = "";
  29. string contentRootPath = _hostEnvironment.ContentRootPath;
  30. string file = "version.txt";
  31. var filePath = contentRootPath + "\\release\\" + app + "\\" + file;
  32. string version = "1.0.0";
  33. try
  34. {
  35. if (System.IO.File.Exists(filePath))
  36. {
  37. version = await System.IO.File.ReadAllTextAsync(filePath);
  38. code = ResultCode.Success;
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. }
  44. ResultViewModel result = ResultHelper.CreateResult(code, version, subCode, subMsg);
  45. return Ok(result);
  46. }
  47. [Route("download")]
  48. [HttpGet]
  49. public FileResult downloadApk(string app = "sfm")
  50. {
  51. string contentRootPath = _hostEnvironment.ContentRootPath;
  52. string file = "app-" + app + "-release.apk";
  53. var addrUrl = contentRootPath + "\\release\\" + app + "\\" + file;
  54. var stream = System.IO.File.OpenRead(addrUrl);
  55. string fileExt = Path.GetExtension(file);
  56. //»ñÈ¡ÎļþµÄContentType
  57. var provider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  58. {
  59. { ".apk","application/vnd.android.package-archive"},
  60. });
  61. var mime = provider.Mappings[fileExt];
  62. return File(stream, mime, Path.GetFileName(addrUrl));
  63. }
  64. }
  65. }