FileTools.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using SnapshotWinFormsApp.Entities.NewHotline;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Net.Http.Headers;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using File = SnapshotWinFormsApp.Entities.NewHotline.File;
  11. namespace SnapshotWinFormsApp.Tools;
  12. public class FileTools
  13. {
  14. /// <summary>
  15. /// 下载网络文件并上传到文件服务器, 返回 File.Id
  16. /// </summary>
  17. /// <param name="file"></param>
  18. /// <param name="cancellationToken"></param>
  19. /// <returns></returns>
  20. public async Task<FileJson> GetNetworkFileAsync(string file, CancellationToken cancellationToken = default)
  21. {
  22. try
  23. {
  24. var fileBytes = await DownloadFileToMemoryAsync(file);
  25. if (fileBytes is null) return new FileJson();
  26. var uploadUrl = ConfigurationManager.AppSettings["FileServerUrl"] + "/file/upload?source=hotline";
  27. var match = Regex.Match(file, "[^/]+\\.[a-zA-Z0-9]+$").Value;
  28. var resultJson = await UploadFileFromMemoryAsync(fileBytes, match, uploadUrl);
  29. var result = resultJson.FromJson<ApiResponse<FileJson>>();
  30. var fileSplit = match.Split('.');
  31. var entity = new File()
  32. {
  33. Additions = result.Result.Id,
  34. FileName = result.Result.FileName,
  35. Path = result.Result.Path,
  36. AllPath = file,
  37. Type = fileSplit[1],
  38. Name = fileSplit[0],
  39. };
  40. return result.Result;
  41. }
  42. catch (Exception e)
  43. {
  44. var message = $"下载网络文件并上传到文件服务器失败: {e.Message}";
  45. }
  46. return new FileJson();
  47. }
  48. /// <summary>
  49. /// 下载文件, 返回字节
  50. /// </summary>
  51. /// <param name="url"></param>
  52. /// <returns></returns>
  53. private async Task<byte[]> DownloadFileToMemoryAsync(string url)
  54. {
  55. try
  56. {
  57. using var client = new HttpClient();
  58. HttpResponseMessage response = await client.GetAsync(url);
  59. response.EnsureSuccessStatusCode();
  60. return await response.Content.ReadAsByteArrayAsync();
  61. }
  62. catch (Exception e)
  63. {
  64. var msg = e.Message;
  65. }
  66. finally
  67. {
  68. }
  69. return null;
  70. }
  71. public async Task<string> UploadFileFromMemoryAsync(byte[] fileBytes, string fileName, string uploadUrl)
  72. {
  73. using HttpClient client = new HttpClient();
  74. using var multipartContent = new MultipartFormDataContent();
  75. var fileContent = new ByteArrayContent(fileBytes);
  76. fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
  77. multipartContent.Add(fileContent, "fileData", fileName);
  78. HttpResponseMessage response = await client.PostAsync(uploadUrl, multipartContent);
  79. response.EnsureSuccessStatusCode();
  80. var result = await response.Content.ReadAsStringAsync();
  81. return result;
  82. }
  83. }