4

I have a file on my desktop for test. I am trying to display it in a view that looks like this:

@{
    ViewBag.Title = "ShowFile";
}

<h2>ShowFile</h2>

The code I am using for the controller is:

   [HttpGet]
        public ActionResult ShowFile(string path)
        {
            path = @"C:\Documents and Settings\nickla\Desktop\nlamarca_06_15.pdf";

            return File(path, "application/pdf", "nlamarca_06_15.pdf");
        }

When I run this code the view displays "undefined" any ideas on what could be wrong here?

3 Answers 3

8

You don't seem to have specified the filename in your path:

public ActionResult ShowFile(string filename)
{
    var path = @"C:\Documents and Settings\nickla\Desktop";
    var file = Path.Combine(path, filename);
    file = Path.GetFullPath(file);
    if (!file.StartsWith(path))
    {
        // someone tried to be smart and sent 
        // ?filename=..\..\creditcard.pdf as parameter
        throw new HttpException(403, "Forbidden");
    }
    return File(file, "application/pdf");
}
Sign up to request clarification or add additional context in comments.

13 Comments

What view are you talking about? There's no view here. The controller action returns the file directly.
do I have do something more to ShowFile.cshtml to display the files contents? Should I remove the view all together?
You could completely remove this view. To invoke your controller action you simply navigate to /home/showfile in your browser (replace home with the name of the controller containing the ShowFile action if necessary).
ahhh - this little nugget of info (the presence of ShowFile.cshtml) reveals the issue now!! slowly but surely teasing a problem out :)
Do you think creating the view is messing this up?
|
2

You are missing the file name in the path. your path only up to the directory. Give the full PDF file name.

public ActionResult ShowFile(string path)
{
   //not sure why you overwrote although you have a parameter to pass the path

    path = @"C:\Documents and Settings\nickla\Desktop\nlamarca_06_15.pdf";
    return File(path, "application/pdf", "nlamarca_06_15.pdf");
}

Assuming the PDF file name you have in that particular directory is nlamarca_06_15.pdf

4 Comments

this returns the word "undefined" to the view
Make sure you have the file exist in that path.
this file is on my desktop, I am running the application via localhost
Try moving the file to a different location and see ( Ex : C:\\nlamarca_06_15.pdf). Make sure to edit your path in the code also. And are you passing the path parameter value in url ?
0

Nick,

Appears at 1st look to be a path issue (no filename, only path), try:

[HttpGet]
public ActionResult ShowFile(string path)
{
    path = @"C:\Documents and Settings\nickla\Desktop\nlamarca_06_15.pdf";

    return File(path, "application/pdf", "pdf_download_name.pdf");
}

the final parameter is purely the name that you want the downloaded file to be given as it 'hits' the users local drive.

[Edit] I see you've updated your question, which invalidates all of the suggestions so far. The only addition that I can see is that you may not have a route setup that handles the path parameter. this question could be a moving target :)

Comments

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.