IAmGroot
IAmGroot

Reputation: 13855

ASP.NET MVC3: Cant find/access file

 String DocLocation = System.AppDomain.CurrentDomain.BaseDirectory + "Files/test.pdf";
        // or
 String DocLocation = Url.Content("~/Files/test.pdf");

 var document = new FileStream(DocLocation, FileMode.Open);
 var mimeType = "application/pdf";
 var fileDownloadName = "download.pdf";

 return File(document, mimeType, fileDownloadName);

The first method is UnauthorizedAccessException.

The second method cant find the file.

I am trying to send a file for download. Using full desktop path seems to work.

Also, how would I display PDF in the browser instead (note, still need download option as not all are pdf)?

Upvotes: 0

Views: 207

Answers (2)

SLaks
SLaks

Reputation: 887453

File() takes a physical path on disk.
Therefore, you can't use Url.Content, since that returns a relative URL for the browser.

Instead, you need Server.MapPath, which converts an application relative path into a full path on the local disk.

Upvotes: 2

Mikael Härsjö
Mikael Härsjö

Reputation: 1036

Try Server.MapPath("~/Files/test.pdf")

Upvotes: 4

Related Questions