查看PDF而不下载它

时间:2017-11-13 12:43:56

标签: pdf razor asp.net-core

现在我的控制器以这种方式返回文件:

[HttpGet]
public IActionResult document(int id)
{
      var byteArray = db.instances.Where(c => c.id == id).FirstOrDefault().document;    
      if (byteArray == null)
      {
            return null;
      }
      Stream stream = new MemoryStream(byteArray);

      return File(stream, "application/pdf", "document.pdf");
}

客户端:

<a href="@Url.Action("document", "Catalog", new { id = Model.id })" target="_blank">Open pdf</a>

我想在不下载的情况下查看PDF,我该怎么办?

1 个答案:

答案 0 :(得分:2)

您应该在回复中附加一个标题,说明它应该显示而不是下载

这样:

[HttpGet]
public IActionResult document(int id)
{
      var byteArray = db.instances.Where(c => c.id == id).FirstOrDefault().document;    
      if (byteArray == null)
      {
            return null;
      }
      Stream stream = new MemoryStream(byteArray);

      Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
      return File(stream, "application/pdf", "document.pdf");
}