DocsUpload.razor 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. @page "/docsupload"
  2. @using System.IO;
  3. @attribute [Authorize]
  4. @*@attribute [Authorize(Roles = "admin")]*@
  5. <EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
  6. <DataAnnotationsValidator />
  7. <ValidationSummary />
  8. <h1>Загрузка материала</h1>
  9. <div style="width: 50%;">
  10. <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document" /></p>
  11. <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
  12. <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
  13. <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
  14. <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
  15. <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
  16. <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
  17. <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
  18. <p>Статус: @status</p>
  19. </div>
  20. </EditForm>
  21. @code {
  22. @inject NavigationManager NavigationManager;
  23. @inject IJSRuntime JsRuntime;
  24. private const string FOLDER_NAME = "articles_storage";
  25. private const long MAX_FILE_SIZE = 5120000; //bytes
  26. private Models.ArticleModel articleModel = new Models.ArticleModel();
  27. private string status;
  28. MemoryStream memoryStream;
  29. private async void HandleValidSubmit()
  30. {
  31. MySQLConnector dbCon = MySQLConnector.Instance();
  32. string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
  33. $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
  34. $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
  35. long id = dbCon.SQLInsert(stringSQL);
  36. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
  37. $"VALUES ('{id}', {1}, {1})";
  38. dbCon.SQLInsert(stringSQL);
  39. dbCon.Close();
  40. string path = System.AppDomain.CurrentDomain.BaseDirectory;
  41. Directory.CreateDirectory(Path.Combine(path, FOLDER_NAME));
  42. string fullpath = Path.Combine(path, FOLDER_NAME, $"{id}_{articleModel.Filename}");
  43. Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
  44. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  45. //byte[] bytes = new byte[memoryStream.Length];
  46. //await memoryStream.ReadAsync(bytes, 0, (int)memoryStream.Length);
  47. //await fs.WriteAsync(bytes, 0, bytes.Length);
  48. await memoryStream.CopyToAsync(fs);
  49. memoryStream.Close();
  50. fs.Close();
  51. Console.WriteLine("Article data saved");
  52. status = "Article data saved";
  53. bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
  54. if (confirmed)
  55. NavigationManager.NavigateTo("docsupload", true);
  56. else
  57. NavigationManager.NavigateTo("");
  58. }
  59. async Task HandleSelection(InputFileChangeEventArgs e)
  60. {
  61. IBrowserFile file = e.File;
  62. if (file != null)
  63. {
  64. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  65. memoryStream = new();
  66. await stream.CopyToAsync(memoryStream);
  67. status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
  68. DocParse docParse = new DocParse();
  69. articleModel = DocParse.ReadPDF(memoryStream);
  70. articleModel.Filename = file.Name;
  71. }
  72. }
  73. }