ASP MVC4如何在控制器的动作方法中发送标题强制pdf文件下载?

时间:2015-09-29 06:23:17

标签: asp.net-mvc-4

我在服务器端有一个控制器,方法

        public ActionResult GetBulletin()
    {

       return File(@"E:\Fileserver\022015.pdf", "application/pdf");
    }

当用户点击网页上的链接时会触发此操作,然后在浏览器窗口中打开该文档。 链接是

<a href="http://www.domain.com/File/GetBulletin">Download</a>

我想做的是强行下载,我读到我们将使用内容处理标题,我的问题是如何让服务器发送此标题?

        Response.AppendHeader("Content-Disposition", @"attachment; filename=E:\Fileserver\022015.pdf");
        return View() ;

当然没有工作。谢谢。

1 个答案:

答案 0 :(得分:1)

您应该只在Response.AppendHeader("Content-Disposition", @"attachment; filename=E:\Fileserver\022015.pdf");语句中设置文件名,使其变为:

Response.AppendHeader("Content-Disposition", @"attachment; filename=022015.pdf");

然后您可以使用File(<file-bytes>, <content-type>)将文件发送到客户端。在你的情况下,这可以这样做:

return File(System.IO.File.ReadAllBytes(@"E:\Fileserver\022015.pdf"), "application/pdf")

相关问题