Я попробовал почти все, что мне кажется. , но это все еще не работает, я совершенно новичок в работе со страницами razor и никогда не буду ничего с ними делать после этого, но мне нужно это сделать.
Test.cshtml.cs:
Код: Выделить всё
using Google.Cloud.DocumentAI.V1;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using Google.Protobuf;
using System.Diagnostics;
namespace MyApp.Namespace
{
public class DocumentViewModel : PageModel
{
public Document ProcessedDocument { get; private set; }
public bool IsDocumentProcessed { get; set; }
public void OnGet()
{
}
public async Task OnPost(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
ModelState.AddModelError("filePath", "File path is required.");
return Page();
}
try
{
// Call to Document AI API
ProcessedDocument = Quickstart(filePath);
IsDocumentProcessed = true;
}
catch (Exception ex)
{
IsDocumentProcessed = false;
ModelState.AddModelError("", "Failed to process document: " + ex.Message);
}
return Page();
}
public Document Quickstart(
string localPath,
string projectId = "...",
string locationId = "eu",
string processorId = "...",
string mimeType = "application/pdf")
{
var client = new DocumentProcessorServiceClientBuilder
{
Endpoint = $"{locationId}-documentai.googleapis.com"
}.Build();
using var fileStream = System.IO.File.OpenRead(localPath);
var rawDocument = new RawDocument
{
Content = ByteString.FromStream(fileStream),
MimeType = mimeType
};
var request = new ProcessRequest
{
Name = ProcessorName.FromProjectLocationProcessor(projectId, locationId, processorId).ToString(),
RawDocument = rawDocument
};
var response = client.ProcessDocument(request);
var document = response.Document;
return document;
}
}
}
Код: Выделить всё
@page
@model MyApp.Namespace.DocumentViewModel
@{
ViewData["Title"] = "Document Processing";
}
Document Processing
Enter file path:
Process Document
@if (Model.IsDocumentProcessed)
{
Processed Document Content
if (Model.ProcessedDocument != null)
{
Text from the document:
@Model.ProcessedDocument.Text
}
else
{
No content available or processing failed.
}
}
else
{
Submit a document for processing.
}
/* Additional CSS styles if needed */
Подробнее здесь: https://stackoverflow.com/questions/784 ... tton-press
Мобильная версия