Search code examples
asp.netpdfreadfile

System.IO.File.ReadAllBytes Access to the path denied


Am running the project on the visual studio 2015, When I tried to read the PDF its giving me the following error;

Access to the path 'E:\FILE\FILEUPLOAD\InnerFile\File' is denied.

Function Defination

    var cd = new System.Net.Mime.ContentDisposition { FileName = "PDF.pdf", Inline = true };

               string contentType = MimeMapping.GetMimeMapping("PDF.pdf");
     Response.AppendHeader("Content-Disposition", cd.ToString()); 
    var innerPath = "InnerFile/File" ;

                FileInfo fi = new FileInfo(PDFUploadRootPath + innerPath + "/PDF.pdf");

byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath);

            return File(bytes, contentType);

NOTE:

  • Given Full permission to user
  • Physically File Exists

I dont understand what to do now please help!

enter image description here


Solution

  • Your FileInfo instance indeed references 'E:\FILE\FILEUPLOAD\InnerFile\File\PDF.pdf':

    FileInfo fi = new FileInfo(PDFUploadRootPath + innerPath + "/PDF.pdf");
    

    but when trying to read the file contents you forgot the file name and only use the path 'E:\FILE\FILEUPLOAD\InnerFile\File':

    byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath);
    

    Thus, also add the file name for reading all file bytes:

    byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath + "/PDF.pdf");
    

    Furthermore, as others have mentioned in comments, you should really use Path.Combine to glue path parts together, not simple string concatenation...