0
 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)?

2
  • 1
    does your web server have access to that file? Commented Feb 7, 2012 at 14:28
  • Unlike my deployed server, there is no IIS User to set permission for. Unless Im missing something Commented Feb 7, 2012 at 14:30

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is the same result location string as my first method. Unauthorised access exception.
I would check file access on the file
Turns out the file had a readonly attribute set. They are being copied off a central file server. I didnt expect this flag to be set, though it makes sense. Thanks for the better Absolute path code.
2

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.

1 Comment

Okay. This is the same result location string as my first method. Unauthorised access exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.