@page "/docsupload"
@using System.IO;
Загрузка материала
Статус: @status
@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;
private string text;
private IBrowserFile file;
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.Author}', '{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}");
FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
byte[] bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
await fs.WriteAsync(bytes, 0, bytes.Length);
stream.Close();
fs.Close();
Console.WriteLine("Article data saved");
status = "Article data saved";
bool confirmed = await JsRuntime.InvokeAsync("confirm", "{Хотите загрузить еще статью?");
if(confirmed)
NavigationManager.NavigateTo("docsupload", true);
else
NavigationManager.NavigateTo("");
}
async Task HandleSelection(InputFileChangeEventArgs e)
{
file = e.File;
if (file != null)
{
status = $"Finished loading {file.Size} bytes from {file.Name}";
//передавать из парсинга ArticleModel!!!
DocParse docParse = new DocParse();
Dictionary docFields = await DocParse.ReadPDF(file);
articleModel.Filename = file.Name;
articleModel.Name = docFields["name"];
//articleModel.PublishDate = docFields["date"];
articleModel.Author = docFields["authors"];
articleModel.Keywords = docFields["keywords"];
articleModel.Annotation = docFields["annotation"];
text = docFields["text"];
}
}
}