UploadData.razor 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. @page "/UploadData"
  2. @using System.ComponentModel.DataAnnotations;
  3. @using System.Text.Json;
  4. @using System.ComponentModel
  5. @using AntDesign.TableModels
  6. @using AntDesign.Form
  7. @using System.Linq
  8. @using System.Net.Http.Headers
  9. @using Microsoft.Extensions.Logging
  10. @inject HttpClient Http
  11. @inject ILogger<UploadData> Logger
  12. @using wispro.sp.share
  13. <style>
  14. .steps-content {
  15. margin-top: 16px;
  16. border: 1px dashed #e9e9e9;
  17. border-radius: 6px;
  18. background-color: #fafafa;
  19. min-height: 200px;
  20. text-align: center;
  21. padding-top: 80px;
  22. }
  23. .steps-action {
  24. margin-top: 24px;
  25. }
  26. </style>
  27. <Form Model="@model" LabelColSpan="10" WrapperColSpan="16" style="margin-left: 50px;margin-right:50px;">
  28. <wispro.sp.web.Share.SPStep CurrentStep="0" />
  29. <div class=" ant-upload ant-upload-select-text ant-upload-drag ant-upload-select">
  30. <FormItem>
  31. <p />
  32. </FormItem>
  33. <FormItem Label="绩效月份" LabelColSpan="10" WrapperColSpan="5">
  34. <DatePicker TValue="DateTime?" Picker="@DatePickerType.Month" @bind-Value="@context.Month" />
  35. </FormItem>
  36. <div tabindex="0" class="ant-upload" style="position:relative;" data-fileid="5c8e2c2b-92b2-4b57-bac4-89c3f44c64d4" role="button" _bl_1728="">
  37. <Upload Action="http://localhost:39476/api/AttachFiles/PostFile" Name="file">
  38. <p class="ant-upload-drag-icon">
  39. <Icon Type="upload" />
  40. </p>
  41. <p class="ant-upload-text">请选择包括绩效事项记录的Excel文件</p>
  42. <p class="ant-upload-hint">
  43. 请从维德流程管理系统中导出上月的申请案件完成事项报表文件,并将其上传到系统开始绩效核算流程
  44. </p>
  45. </Upload>
  46. <div class="file-input-zone">
  47. <InputFile OnChange="OnInputFileChange" multiple />
  48. Get me a file!
  49. </div>
  50. </div>
  51. <p />
  52. <FormItem LabelColSpan="20" WrapperColSpan="16">
  53. <Button Type="@ButtonType.Primary" HtmlType="submit">
  54. 上传
  55. </Button>
  56. </FormItem>
  57. </div>
  58. </Form>
  59. <style>
  60. .file-input-zone {
  61. display: flex;
  62. align-items: center;
  63. justify-content: center;
  64. background-color: blue;
  65. color: white;
  66. cursor: pointer;
  67. position: relative;
  68. width: 120px;
  69. height: 30px;
  70. }
  71. .file-input-zone:hover {
  72. background-color: lightblue;
  73. }
  74. .file-input-zone input[type=file] {
  75. position: absolute;
  76. width: 100%;
  77. height: 100%;
  78. opacity: 0;
  79. cursor: pointer;
  80. }
  81. </style>
  82. @code {
  83. public class Model
  84. {
  85. public DateTime? Month { get; set; } = DateTime.Now.AddMonths(-1);
  86. }
  87. private Model model = new Model();
  88. private void OnFinish(EditContext editContext)
  89. {
  90. //Console.WriteLine($"Success:{JsonSerializer.Serialize(model)}");
  91. }
  92. private void OnFinishFailed(EditContext editContext)
  93. {
  94. //Console.WriteLine($"Failed:{JsonSerializer.Serialize(model)}");
  95. }
  96. private List<File> files = new();
  97. private List<UploadResult> uploadResults = new();
  98. private int maxAllowedFiles = 3;
  99. private bool shouldRender;
  100. protected override bool ShouldRender() => shouldRender;
  101. private async void OnSingleCompleted(AntDesign.UploadInfo uploadInfo)
  102. {
  103. long maxFileSize = 1024 * 1024 * 15;
  104. //Stream fs = uploadInfo.File.OpenReadStream(maxFileSize);
  105. //await fs.CopyToAsync(memoryStream);
  106. //Assembly assembly = Assembly.Load(memoryStream.ToArray());
  107. }
  108. private async System.Threading.Tasks.Task OnInputFileChange(InputFileChangeEventArgs e)
  109. {
  110. shouldRender = false;
  111. long maxFileSize = 1024 * 1024 * 15;
  112. var upload = false;
  113. using var content = new MultipartFormDataContent();
  114. foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
  115. {
  116. if (uploadResults.SingleOrDefault(
  117. f => f.FileName == file.Name) is null)
  118. {
  119. try
  120. {
  121. var fileContent =
  122. new StreamContent(file.OpenReadStream(maxFileSize));
  123. fileContent.Headers.ContentType =
  124. new MediaTypeHeaderValue(file.ContentType);
  125. files.Add(new() { Name = file.Name });
  126. content.Add(
  127. content: fileContent,
  128. name: "\"files\"",
  129. fileName: file.Name);
  130. upload = true;
  131. }
  132. catch (Exception ex)
  133. {
  134. Logger.LogInformation(
  135. "{FileName} not uploaded (Err: 6): {Message}",
  136. file.Name, ex.Message);
  137. uploadResults.Add(
  138. new()
  139. {
  140. FileName = file.Name,
  141. ErrorCode = 6,
  142. Uploaded = false
  143. });
  144. }
  145. }
  146. }
  147. if (upload)
  148. {
  149. var response = await Http.PostAsync("http://localhost:39476/api/AttachFiles/PostFile", content);
  150. var newUploadResults = await response.Content
  151. .ReadFromJsonAsync<IList<UploadResult>>();
  152. uploadResults = uploadResults.Concat(newUploadResults).ToList();
  153. }
  154. shouldRender = true;
  155. }
  156. private static bool FileUpload(IList<UploadResult> uploadResults,
  157. string fileName, ILogger<UploadData> logger, out UploadResult result)
  158. {
  159. result = uploadResults.SingleOrDefault(f => f.FileName == fileName);
  160. if (result is null)
  161. {
  162. logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
  163. result = new();
  164. result.ErrorCode = 5;
  165. }
  166. return result.Uploaded;
  167. }
  168. private class File
  169. {
  170. public string Name { get; set; }
  171. }
  172. }