简体   繁体   English

如何使用控制器打开pdf文件? ASP.NET MVC

[英]How to open a pdf file using the controller? Asp.net MVC

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.我正在尝试使用控制器在我的应用程序中打开位于文件夹(Content/extras)内的 PDF 文件,但对我不起作用。 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'" “找不到文件'C:/..../Content/extras/PDFName'”异常详细信息:System.IO.FileNotFoundException:找不到文件'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.所以基本上它说它找不到该文件,但该文件在该位置是 100% 并且名称是正确的。 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.虽然我确实注意到这是试图找到“PDFName”而不是“PDFName.pdf”,所以也许这就是错误所在,并且控制器中的“MimeMapping”没有正确编码。

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 cshtml 文件

<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.'我再次猜测是“MimeMapping”控制器代码不正确,因为它似乎没有在寻找“.pdf”。 and is only looking for the PDFName, but not really sure what's wrong.并且只是在寻找 PDFName,但不确定出了什么问题。 Any suggestions?有什么建议? Thanks.谢谢。

I finally figured it out.我终于弄明白了。 I just needed to add the .pdf extention into the PDFName on the 'path' variable.我只需要将 .pdf 扩展名添加到“路径”变量上的 PDFName 中。 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.混乱是因为我认为“路径”只是只有 PDFName 而不是 .pdf 扩展名的路径,然后“mime”会有“.pdf”扩展名。 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.但是,不,“路径”应该包含文件包含 .pdf 扩展名的整个路径,然后“mime”变量只是根据我的理解,只是为了识别文件的类型。

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.唯一不能正常工作的是路由 URL,但这是另一个线程中的另一个问题。 But at least the pdf file is showing correctly which was the purpose of this question.但至少 pdf 文件显示正确,这是这个问题的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM