I have an application which will allow a user to convert a .pdf file to a .txt file. At this point, I am simply trying to open a file with the Aspose.pdf package. I am developing in Visual Studio 2019 on a Mac. I have a view with the following code:
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="Index">
<div class="form-group">
<div class="col-md-10">
<p>Convert a .pdf file:</p>
<input type="file" accept=".pdf" name="files" />
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Upload" />
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<label>@Model.SendMessage</label>
</div>
</div>
</form>
When I run the application this is what is displayed in the browser:
When I click on the Choose File button I get the standard open file dialog:
I click on the second .pdf file and click on open and I get this showing in the browser:
When I click on the Upload button, the file name is passed back to the controller. This is the code that I have in the controller:
[HttpPost]
public async Task<IActionResult> Index(List<IFormFile> files)
{
PassData localMessage = new PassData();
localMessage.SendMessage = "Your file failed to converted";
var size = files.Sum(f => f.Length);
var filePaths = new List<string>();
foreach (var formFile in files)
{
var filePath = Path.GetFullPath(formFile.FileName);
// Open document
Document pdfDocument = new Document(filePath);
// Instantiate a TextAbsorber class object for extracting the text
TextAbsorber textAbsorber = new TextAbsorber(new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure), new TextSearchOptions(new Rectangle(5, 5, 50, 50)));
// Call the Accept() function to parse all the pages for reading text
pdfDocument.Pages.Accept(textAbsorber);
// Get extracted text as string
string ExtractedText = textAbsorber.Text;
if (ExtractedText.Length > 0)
{
localMessage.SendMessage = "Your file has been converted";
}
}
return View();
}
}
I have a breakpoint set on the line of code where I set up the filePath variable. Once that line of code executes, I look at the value of filePath and this is the value
/Users/jonathansmall/Projects/pdfConverterV2/pdfConverterV2/FinalTaxRoll2022_23.pdf
Remember, I am on a Mac.
The code in the controller continues to execute and I get an internal server error when trying to open the document.
This is the error:
An unhandled exception occurred while processing the request. FileNotFoundException: Could not find file '/Users/jonathansmall/Projects/pdfConverterV2/pdfConverterV2/Record Grouch - Lease FULLY EXECUTED.pdf'.
I dont understand why the system says it can not find the file if the system was the one that provided me with the full path and file name.
Any suggestions?
Thanks.