繁体   English   中英

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

[英]How can I open a pdf file directly in my browser?

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

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

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>

当我鼠标悬停时,这里的链接是链接:

在此处输入图片说明

我的代码的问题是无法在浏览器中查看pdf文件,但我收到一条消息,询问我是否要打开或保存文件。

在此处输入图片说明

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

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

在此处输入图片说明

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

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

您收到要求您打开或保存文件的消息的原因是您指定了文件名。 如果您不指定文件名,PDF 文件将在您的浏览器中打开。

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

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

或者,如果您需要指定文件名,则必须这样做:

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

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

不要返回File ,而是尝试返回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;
}

将您的代码更改为:

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

如果您读取存储在数据库图像列中的文件,您可以像这样使用:

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

如果您使用Rotativa包生成 PDF,则不要像下面的示例那样使用FileName属性为文件命名。

 return new PartialViewAsPdf("_JcPdfGenerator", pdfModel);

希望这对某人有帮助。

尽管以前的帖子通常是正确的; 我认为他们中的大多数都不是最佳实践! 我想建议将操作返回类型更改为FileContentResult并使用return new FileContentResult(fileContent, "application/pdf"); 在动作体结束时。

是的,您可以简单地通过重定向来做到这一点。 它像你需要的那样结束扩展,.pdf ..

protected void OpenPdfPdf_Click(object sender, EventArgs e)
        {
            Response.Redirect("jun.pdf");
        }

或另一种方法,它像 .aspx 页面一样打开--

 protected void OpenPdf_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("jun.pdf");
            //or you want to load from url change path to
//string path="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";   
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(path);
            if (buffer != null)
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }
        }

暂无
暂无

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

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