0

I'm trying to open a PDF file located inside the folder (Content/extras) in my app using the controller and is not working for me. The error I'm receiving is

"Could not find file 'C:/..../Content/extras/PDFName'" Exception details: System.IO.FileNotFoundException: Could not find file 'C:/..../Content/extras/PDFName'"

So basically it says that it can't find the file but the file is 100% in that location and the name is correct. I do notice though that is trying to find 'PDFName' instead of 'PDFName.pdf' so maybe that's what's wrong and is the 'MimeMapping' in the controller that is not coded correctly.

Here is my code:

Controller

public FileResult PDFFlyer()
{
    string path = Server.MapPath(String.Format("~/Content/extras/PDFName"));

    string mime = MimeMapping.GetMimeMapping(path);

    return File(path, mime);
}

RouteConfig

    routes.MapRoute(
        name: "PDFFlyer",
        url: "{filename}",
        defaults: new { controller = "PDF", action = "PDFFlyer", filename = 
        UrlParameter.Optional }
    );

The cshtml file

<a class="dropdown-item" href="@Url.Action("PDFFlyer", "PDF")" target="_blank">PDF Flyer</a>

What am I doing wrong? Again I'm guessing is the 'MimeMapping' Controller code that is incorrect because it doesn't seem to be looking for the '.pdf.' and is only looking for the PDFName, but not really sure what's wrong. Any suggestions? Thanks.

6
  • Is PDFName just a mock or the actual filename without the .pdf extension? Commented Mar 15, 2020 at 19:23
  • Is a mock and is to reflect the filename of the pdf file. Commented Mar 15, 2020 at 19:25
  • In your question text you do mention that the extension is missing, that is why I am asking - so what is true now? Commented Mar 15, 2020 at 21:27
  • I don't understand your question Commented Mar 15, 2020 at 22:23
  • I've asked you if "PDFName" is just a mock, to whic you answered "Yes", yet in your question you are wondering why the extension to "PDFName" is missing. If it is just a mock for your question - all good. If not, then why doesn't it have a pdf extension in its filename like you said in your question? Commented Mar 16, 2020 at 6:50

1 Answer 1

2

I finally figured it out. I just needed to add the .pdf extention into the PDFName on the 'path' variable. The confusion was cause I thought the 'path' was only the path with just the PDFName and not the .pdf extension, and then the 'mime' would have the '.pdf' extention. But no, the 'path' should have the whole path of were the file is INCLUDING the .pdf extention, and then the 'mime' variable is marely, as I understand, just to identity the type of the file.

So, the Controller, which is the only thing I changed for it to work, looks like this:

public FileResult PDFFlyer()
{
    //include the .pdf extention at the end
    string path = Server.MapPath(String.Format("~/Content/extras/PDFName.pdf")); 

    string mime = MimeMapping.GetMimeMapping(path);

    return File(path, mime);
}

The only thing that doesn't work correctly is the Route URL, but that's for another question in another thread. But at least the pdf file is showing correctly which was the purpose of this question.

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

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.