Ace Grace
Ace Grace

Reputation: 641

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

Upvotes: 1

Views: 1847

Answers (2)

MartinHN
MartinHN

Reputation: 19772

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

Upvotes: 4

Rahul Neekhra
Rahul Neekhra

Reputation: 810

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

Upvotes: 0

Related Questions