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

时间:2021-11-09 21:15:42

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.

我想直接在浏览器中查看PDF文件。我知道这个问题已被提出,但我找不到适用于我的解决方案。

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);
}

Here is my view:

这是我的观点:

@{
   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文件没有在浏览器中查看,但我收到一条消息,询问我是否打开或保存文件。

如何直接在浏览器中打开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.

我知道这是可能的,我的浏览器支持它,因为我已经用另一个网站测试它,允许我直接在我的浏览器中查看pdf。

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

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

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

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

如您所见,生成的链接存在差异。我不知道这是否有用。

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

知道如何直接在浏览器中查看我的pdf?

6 个解决方案

#1


15  

Instead of returning a File, try returning a FileStreamResult

而不是返回文件,尝试返回FileStreamResult

public ActionResult GetPdf(string fileName)
{
    var fileStream = new FileStream("~/Content/files/" + fileName, 
                                     FileMode.Open,
                                     FileAccess.Read
                                   );
    var fsResult = new FileStreamResult(fileStream, "application/pdf");
    return fsResult;
}

#2


29  

The accepted answer is wrong. 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.

接受的答案是错误的。您收到要求您打开或保存文件的消息的原因是您指定了文件名。如果未指定文件名,则将在浏览器中打开PDF文件。

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");
}

#3


12  

Change your code to this :

将您的代码更改为:

       Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
       return File(filePath, "application/pdf");

#4


1  

If you read the file stored in database image column, you can use like this:

如果您读取存储在数据库映像列中的文件,则可以使用如下所示:

public ActionResult DownloadFile(int id)
{
    using (var db = new DbContext())
    {
        var data =
            db.Documents.FirstOrDefault(m => m.ID == id);
        if (data == null) return HttpNotFound();
        Response.AppendHeader("content-disposition", "inline; filename=filename.pdf");
        return new FileStreamResult(new MemoryStream(data.Fisier.ToArray()), "application/pdf");
    }
}

#5


0  

If you are using Rotativa package to generate PDF, Then don't put a name to file with FileName attribute like below example.

如果您使用Rotativa包生成PDF,那么请不要将名称放在FileName属性文件中,如下例所示。

 return new PartialViewAsPdf("_JcPdfGenerator", pdfModel);

Hope this is helpful to someone.

希望这对某人有帮助。

#6


-1  

Although previous posts are often correct; I think most of them are not best practice! I'd like to suggest to change action return types to FileContentResult and usereturn new FileContentResult(fileContent, "application/pdf"); at the end of action body.

虽然之前的帖子通常是正确的;我认为大多数都不是最好的做法!我想建议将操作返回类型更改为FileContentResult并使用返回新的FileContentResult(fileContent,“application / pdf”);在行动结束时。

#1


15  

Instead of returning a File, try returning a FileStreamResult

而不是返回文件,尝试返回FileStreamResult

public ActionResult GetPdf(string fileName)
{
    var fileStream = new FileStream("~/Content/files/" + fileName, 
                                     FileMode.Open,
                                     FileAccess.Read
                                   );
    var fsResult = new FileStreamResult(fileStream, "application/pdf");
    return fsResult;
}

#2


29  

The accepted answer is wrong. 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.

接受的答案是错误的。您收到要求您打开或保存文件的消息的原因是您指定了文件名。如果未指定文件名,则将在浏览器中打开PDF文件。

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");
}

#3


12  

Change your code to this :

将您的代码更改为:

       Response.AppendHeader("Content-Disposition","inline;filename=xxxx.pdf");
       return File(filePath, "application/pdf");

#4


1  

If you read the file stored in database image column, you can use like this:

如果您读取存储在数据库映像列中的文件,则可以使用如下所示:

public ActionResult DownloadFile(int id)
{
    using (var db = new DbContext())
    {
        var data =
            db.Documents.FirstOrDefault(m => m.ID == id);
        if (data == null) return HttpNotFound();
        Response.AppendHeader("content-disposition", "inline; filename=filename.pdf");
        return new FileStreamResult(new MemoryStream(data.Fisier.ToArray()), "application/pdf");
    }
}

#5


0  

If you are using Rotativa package to generate PDF, Then don't put a name to file with FileName attribute like below example.

如果您使用Rotativa包生成PDF,那么请不要将名称放在FileName属性文件中,如下例所示。

 return new PartialViewAsPdf("_JcPdfGenerator", pdfModel);

Hope this is helpful to someone.

希望这对某人有帮助。

#6


-1  

Although previous posts are often correct; I think most of them are not best practice! I'd like to suggest to change action return types to FileContentResult and usereturn new FileContentResult(fileContent, "application/pdf"); at the end of action body.

虽然之前的帖子通常是正确的;我认为大多数都不是最好的做法!我想建议将操作返回类型更改为FileContentResult并使用返回新的FileContentResult(fileContent,“application / pdf”);在行动结束时。