1

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

Ace Grace
  • 631
  • 1
  • 7
  • 21

2 Answers2

4

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

Community
  • 1
  • 1
MartinHN
  • 19,542
  • 19
  • 89
  • 131
0

You can do that. For that you need to create virtual direct in IIS into your website and then you can use that virtual director which you can map to the folder other than the project folder

Rahul Neekhra
  • 780
  • 1
  • 9
  • 39
  • So I can't access the folder directly at c:\data\pdffiles from within my wen app if I set the permissions for IIS APPPOOL{Application pool name}? – Ace Grace Jan 10 '17 at 12:51
  • Yes. Reason being IIS APPPool also look at build folders which are included in the project. – Rahul Neekhra Jan 11 '17 at 06:34
  • Even this is good practice to setup virtual directory as it is easy to manage and it is also industry wide standard to manage the folders out side the project or website – Rahul Neekhra Jan 11 '17 at 06:36