如何直接在浏览器中打开pdf文件

如何直接在浏览器中打开pdf文件

本文介绍了如何直接在浏览器中打开pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接在浏览器中查看PDF文件.我知道已经问过这个问题,但是我没有找到适合我的解决方案.

I would like to view a PDF file directly in my browser. I know this question is already asked but I haven't found a solution that works for me.

到目前为止,这是我动作的控制器代码:

Here is my action's controller code so far:

public ActionResult GetPdf(string fileName)
{
    string filePath = "~/Content/files/" + fileName;
    return File(filePath, "application/pdf", fileName);
}

这是我的观点:

@{
   doc = "Mode_d'emploi.pdf";
}

<p>@Html.ActionLink(UserResource.DocumentationLink, "GetPdf", "General", new { fileName = doc }, null)</p>

当我将鼠标悬停在此处时,链接是链接:

When I mouse hover the link here is the link:

我的代码存在问题,即pdf文件未在浏览器中查看,但是我收到一条消息,询问是否要用魔杖打开或保存该文件.

The problem with my code is that the pdf file is not viewed in the browser but I get a message asking me if I wand to open or save the file.

我知道这是可能的,我的浏览器也支持它,因为我已经在另一个网站上对其进行了测试,可以直接在浏览器中查看pdf.

I know it is possible and my browser support it because I already test it with another website allowing me to view pdf directly in my browser.

例如,这是当我将鼠标悬停在另一个网站上的链接时的链接:

For example, here is the link when I mouse hover a link (on another website):

如您所见,生成的链接有所不同.我不知道这是否有用.

As you can see there is a difference in the generated link. I don't know if this is useful.

有什么想法可以直接在浏览器中查看我的pdf吗?

Any idea how can I view my pdf directly in the browser?

推荐答案

收到消息提示您打开或保存文件的原因是您指定了文件名.如果您未指定文件名,则将在浏览器中打开PDF文件.

The reason you're getting a message asking you to open or save the file is that you're specifying a filename. If you don't specify the filename the PDF file will be opened in your browser.

因此,您所需要做的就是将操作更改为此:

So, all you need to do is to change your action to this:

public ActionResult GetPdf(string fileName)
{
    string filePath = "~/Content/files/" + fileName;
    return File(filePath, "application/pdf");
}

或者,如果您需要指定文件名,则必须采用以下方式:

Or, if you need to specify a filename you'll have to do it this way:

public ActionResult GetPdf(string fileName)
{
    string filePath = "~/Content/files/" + fileName;
    Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);

    return File(filePath, "application/pdf");
}

这篇关于如何直接在浏览器中打开pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:51