12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- @page "/docsupload"
- @using System.IO;
- @attribute [Authorize]
- @*@attribute [Authorize(Roles = "admin")]*@
- <EditForm Model="@articleModel" OnValidSubmit="@HandleValidSubmit">
- <DataAnnotationsValidator />
- <ValidationSummary />
- <h1>Загрузка материала</h1>
- <div style="width: 50%;">
- <p><InputFile id="inputDefault" OnChange="@HandleSelection" accept="application/pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document" /></p>
- <p><InputText id="article_name" class="form-control" @bind-Value="articleModel.Name" placeholder="Наименование статьи" /></p>
- <p><InputDate id="date_publish" class="form-control" @bind-Value="articleModel.PublishDate" placeholder="Дата издания" /></p>
- <p><InputText id="author" class="form-control" @bind-Value="articleModel.Authors" placeholder="Автор" /></p>
- <p><InputTextArea id="keywords" class="form-control" @bind-Value="articleModel.Keywords" placeholder="Ключевые слова" /></p>
- <p><InputTextArea rows="5" id="annotation" class="form-control" @bind-Value="articleModel.Annotation" placeholder="Аннотация" /></p>
- <p><InputTextArea rows="10" id="text" class="form-control" @bind-Value="articleModel.Text" placeholder="Текст" /></p>
- <p><button class="btn btn-primary" type="submit">Загрузить</button></p>
- <p>Статус: @status</p>
- </div>
- </EditForm>
- @code {
- @inject NavigationManager NavigationManager;
- @inject IJSRuntime JsRuntime;
- private const string FOLDER_NAME = "articles_storage";
- private const long MAX_FILE_SIZE = 5120000; //bytes
- private Models.ArticleModel articleModel = new Models.ArticleModel();
- private string status;
- MemoryStream memoryStream;
- private async void HandleValidSubmit()
- {
- MySQLConnector dbCon = MySQLConnector.Instance();
- string stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords)" +
- $"VALUES ('{articleModel.Filename}', '{articleModel.Name}', '{articleModel.Authors}', '{articleModel.PublishDate.ToString("yyyy-MM-dd")}'," +
- $"'{articleModel.Annotation}', '{articleModel.Keywords}')";
- long id = dbCon.SQLInsert(stringSQL);
- stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id)" +
- $"VALUES ('{id}', {1}, {1})";
- dbCon.SQLInsert(stringSQL);
- dbCon.Close();
- string path = System.AppDomain.CurrentDomain.BaseDirectory;
- Directory.CreateDirectory(Path.Combine(path, FOLDER_NAME));
- string fullpath = Path.Combine(path, FOLDER_NAME, $"{id}_{articleModel.Filename}");
- Console.WriteLine($"memorystream: {memoryStream.Length}, filename: {articleModel.Filename}");
- FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
- //byte[] bytes = new byte[memoryStream.Length];
- //await memoryStream.ReadAsync(bytes, 0, (int)memoryStream.Length);
- //await fs.WriteAsync(bytes, 0, bytes.Length);
- await memoryStream.CopyToAsync(fs);
- memoryStream.Close();
- fs.Close();
- Console.WriteLine("Article data saved");
- status = "Article data saved";
- bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Хотите загрузить еще статью?");
- if (confirmed)
- NavigationManager.NavigateTo("docsupload", true);
- else
- NavigationManager.NavigateTo("");
- }
- async Task HandleSelection(InputFileChangeEventArgs e)
- {
- IBrowserFile file = e.File;
- if (file != null)
- {
- Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
- memoryStream = new();
- await stream.CopyToAsync(memoryStream);
- status = $"Finished loading {memoryStream.Length} bytes from {file.Name}";
- DocParse docParse = new DocParse();
- articleModel = DocParse.ReadPDF(memoryStream);
- articleModel.Filename = file.Name;
- }
- }
- }
|