Search code examples
asp.netiis-8folder-permissions

Is it possible for an ASP.NET MVC application to read files from a folder elsewhere on the web server?


I have an ASP.NET MVC web app located in c:\inetpub\sites\website

Within it, I have an option to download PDF files.

The PDF files are stored elsewhere e.g. c:\data\pdffiles

Can I read then from the c:\data\pdffiles folder from within my web app if I set the correct permissions with

IIS APPPOOL{Application pool name}

Regards


Solution

  • Yes, you can do that from an ASP.NET MVC controller-action like this:

    public FileResult Download()
    {
        string filePath = @"c:\data\pdffiles\doc.pdf";
        byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
        string fileName = Path.GetFileName(filePath);
    
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
    }
    

    Even shorter - if you don't want to override the filename when downloaded.

    public FileResult Download()
    {
        string filePath = @"c:\data\pdffiles\doc.pdf";
    
        return File(filePath, MimeMapping.GetMimeMapping(filePath));
    }
    

    If you need custom MIME types, see this answer: https://stackoverflow.com/a/14108040/2972