rimi
rimi

Reputation: 775

Access to path denied for files in WWWRoot folder

I am trying to access two folders that resides inside wwwRoot. The folders are "BlankPDFFiles" and "FilledPDFFiles". I am trying to get the blank PDF files that resides inside BlankPDFFiles folder and write some data inside the file and then save it to the folder FilledPDFFiles. This is the structure of my solution:

enter image description here

when I am trying to access the blank PDF file, I am getting below error:

enter image description here

Below is my code

 public class PDFController : Controller
    {
        private readonly IEmployeeService _employeeService;
        public readonly IConfiguration _configuration;
        public readonly ILogger _logger;
        private readonly IWebHostEnvironment _environment;
        public PDFController(IEmployeeService employeeService, IConfiguration configuration, ILogger<PDFController> logger, IWebHostEnvironment environment)
        {
            _employeeService = employeeService;
            _configuration = configuration;
            _logger = logger;
            _environment = environment;
        }

        public async Task<IActionResult> Index()
        {
          
            await PopulatePDFDoc();
            return View();
        }

  public  async Task PopulatePDFDoc()
        {
            AckPackage.Data.PDFPopulate.DocPDF doc = new Data.PDFPopulate.DocPDF();
            string pdfLic = _configuration["PDFLicense"].ToString();
            string filledPDF = Path.Combine(_environment.WebRootPath, "FilledPDFFiles");
            string blankPDF = Path.Combine(_environment.WebRootPath, "BlankPDFFiles");
            EmployeeInfo employee =  await _employeeService.GetEmployeeByEmployeeNumber(up.EmployeeId);
            await doc.popolatePDFDoc(pdfLic, filledPDF, blankPDF, employee);

        }

This is what I have in populatePDFDoc method:

public async Task  popolatePDFDoc(string PDFLic, string filledPDF, string blankPDF, EmployeeInfo employee)
        {
            
            string pathToFile = filledPDF + "_Package"+ "_" + employee.EmployeeNumber;
            bool validLicense = BitMiracle.Docotic.LicenseManager.HasValidLicense;
           
            **using (PdfDocument pdf = new PdfDocument(blankPDF))**
            {
                foreach (PdfControl control in pdf.GetControls())
                {
                    switch (control.Name)
                    {
                        case "EmpID":
                            ((PdfTextBox)control).Text = employee.EmployeeNumber;
                            break;
                        case "Last Name":
                            ((PdfTextBox)control).Text = employee.LastName;
                            break;
                    }
                }
                pdf.Save(pathToFile);
            }

I am getting an error at this line in popolatePDFDoc

 using (PdfDocument pdf = new PdfDocument(blankPDF))

I am using third party vendor tool to populate PDF file.

Upvotes: 0

Views: 886

Answers (1)

user3163495
user3163495

Reputation: 3682

This has nothing to do with the PDF library vendor, and is probably because your web application's exe is running from a directory outside of the path that blankPDF points to.

You can see the directory your application is running from by putting a call to Environment.CurrentDirectory somewhere before the exception occurs and putting a breakpoint on it to see it.

If you are developing locally and getting this error, your application is probably running in the folder C:\AllITProjects\AckPackage\bin\Debug\net7.0.

The wwwroot folder is not always writable from the application, depending on how you are running the application. However, the project folder is always writable, since the project folder is where your application's exe resides. So, here are steps to move your PDF folders into the project folder, which will ensure they remain writable:

1. Move both the BlankPDFFiles and FilledPDFFiles folders out of the wwwroot folder, and into the project folder (the folder where your .csproj file is located).

2. Open the .csproj file in notepad, and add the following line that tells the compiler to copy your PDF folders whenever you compile:

<ItemGroup>
    <None Include="BlankPDFFiles\**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
    <None Include="FilledPDFFiles\**" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
</ItemGroup>

3. Change filledPDF and blankPDF to use _environment.ContentRootPath:

string filledPDF = Path.Combine(_environment.ContentRootPath, "FilledPDFFiles");
string blankPDF = Path.Combine(_environment.ContentRootPath, "BlankPDFFiles");

4. You will have to create an action in a controller that specifically serves the PDF from the filledPDF path, as it will no longer reside in the wwwroot folder.

Let me know if you need more help, especially with Step #4.

Upvotes: 2

Related Questions