0

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:

enter image description here

When I click on the Choose File button I get the standard open file dialog:

enter image description here

I click on the second .pdf file and click on open and I get this showing in the browser:

enter image description here

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.

Jonathan Small
  • 1,027
  • 3
  • 18
  • 40
  • Path.GetFullPath(formFile.FileName) will just prepend the current directory to the filename you'll get from an uploaded file. You need to read the uploaded file as a stream, or save it somewhere else. – CodeCaster Oct 29 '22 at 21:15
  • @CodeCaster I added this line after building the filePath variable: FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); I still get an internal server error stating FileNotFoundException: Could not find file '/Users/jonathansmall/Projects/pdfConverterV2/pdfConverterV2/Record Grouch - Lease FULLY EXECUTED.pdf'. – Jonathan Small Oct 29 '22 at 21:24
  • The file does not exist at that path. Read the IFormFile docs, use OpenReadStream() to read it from its temporarily uploaded location. – CodeCaster Oct 29 '22 at 21:27

0 Answers0