FileController.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using XYY.Core.Standard.AliYun;
  9. using XYY.Core.Standard.Mvc;
  10. namespace XYY.Api.Order.Controllers
  11. {
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. public class FileController : ApiControllerBase
  15. {
  16. IAliYunPostFileSerivce aliYunPostFileSerivce;
  17. public FileController(IAliYunPostFileSerivce aliYunPostFileSerivce)
  18. {
  19. this.aliYunPostFileSerivce = aliYunPostFileSerivce;
  20. }
  21. [AllowAnonymous]
  22. public async Task<IActionResult> UpdateLoadFile()
  23. {
  24. //生成Guid文件防止重复
  25. var filelist = (await Request.ReadFormAsync()).Files;
  26. if (filelist != null && filelist.Count > 0)
  27. {
  28. var file = filelist[0];
  29. string fielName = Guid.NewGuid() + file.FileName.Substring(file.FileName.LastIndexOf('.'));
  30. string url =
  31. aliYunPostFileSerivce.PostHttpFile(file.OpenReadStream(), fielName);
  32. return Content(url);
  33. }
  34. else
  35. {
  36. return Error("请上传一个文件");
  37. }
  38. }
  39. [AllowAnonymous]
  40. public async Task<IActionResult> UpdateLoadFileRestore(string q)
  41. {
  42. System.Net.WebClient webClient = new System.Net.WebClient();
  43. var data = await webClient.DownloadDataTaskAsync(q);
  44. Uri uri = new Uri(q);
  45. string fileName = HttpUtility.UrlDecode(uri.Segments.Last());
  46. return File(fileName, data);
  47. }
  48. }
  49. }