UploadData.razor 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  91. private void OnFinishFailed(EditContext editContext)
  92. {
  93. }
  94. private List<File> files = new();
  95. private List<UploadResult> uploadResults = new();
  96. private int maxAllowedFiles = 3;
  97. private bool shouldRender;
  98. protected override bool ShouldRender() => shouldRender;
  99. private async void OnSingleCompleted(AntDesign.UploadInfo uploadInfo)
  100. {
  101. long maxFileSize = 1024 * 1024 * 15;
  102. //Stream fs = uploadInfo.File.OpenReadStream(maxFileSize);
  103. //await fs.CopyToAsync(memoryStream);
  104. //Assembly assembly = Assembly.Load(memoryStream.ToArray());
  105. }
  106. private async System.Threading.Tasks.Task OnInputFileChange(InputFileChangeEventArgs e)
  107. {
  108. shouldRender = false;
  109. long maxFileSize = 1024 * 1024 * 15;
  110. var upload = false;
  111. using var content = new MultipartFormDataContent();
  112. foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
  113. {
  114. if (uploadResults.SingleOrDefault(
  115. f => f.FileName == file.Name) is null)
  116. {
  117. try
  118. {
  119. var fileContent =
  120. new StreamContent(file.OpenReadStream(maxFileSize));
  121. fileContent.Headers.ContentType =
  122. new MediaTypeHeaderValue(file.ContentType);
  123. files.Add(new() { Name = file.Name });
  124. content.Add(
  125. content: fileContent,
  126. name: "\"files\"",
  127. fileName: file.Name);
  128. upload = true;
  129. }
  130. catch (Exception ex)
  131. {
  132. Logger.LogInformation(
  133. "{FileName} not uploaded (Err: 6): {Message}",
  134. file.Name, ex.Message);
  135. uploadResults.Add(
  136. new()
  137. {
  138. FileName = file.Name,
  139. ErrorCode = 6,
  140. Uploaded = false
  141. });
  142. }
  143. }
  144. }
  145. if (upload)
  146. {
  147. var response = await Http.PostAsync("http://localhost:39476/api/AttachFiles/PostFile", content);
  148. var newUploadResults = await response.Content
  149. .ReadFromJsonAsync<IList<UploadResult>>();
  150. uploadResults = uploadResults.Concat(newUploadResults).ToList();
  151. }
  152. shouldRender = true;
  153. }
  154. private static bool FileUpload(IList<UploadResult> uploadResults,
  155. string fileName, ILogger<UploadData> logger, out UploadResult result)
  156. {
  157. result = uploadResults.SingleOrDefault(f => f.FileName == fileName);
  158. if (result is null)
  159. {
  160. logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
  161. result = new();
  162. result.ErrorCode = 5;
  163. }
  164. return result.Uploaded;
  165. }
  166. private class File
  167. {
  168. public string Name { get; set; }
  169. }
  170. }