如何通过点击按钮从文件下载PDF文件?

时间:2014-06-25 06:52:38

标签: c# pdf

我想在点击按钮上下载PDF文件。我尝试过这样:

 protected void lbtnDownload_Click(object sender, EventArgs e)
    {
        if (lbtnDownload.Text != string.Empty)
        {
            else if (lbtnDownload.Text.EndsWith(".pdf"))
            {
                Response.ContentType = "application/pdf";
            }

            string filePath = lbtnDownload.Text;

            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
            Response.TransmitFile(HttpContext.Current.Server.MapPath("~/") + "\\PDF\\" + filePath );
            Response.End();
        }

    }

但没有任何反应,我的意思是当我调试它时没有异常,但文件没有下载。

有谁知道我的错误在哪里,以及我应该怎么做才能下载PDF?

1 个答案:

答案 0 :(得分:0)

试试这个

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if(string.IsNullOrEmpty(txtName.Text)) return;            

        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + txtName.Text);
        string filePath = Server.MapPath(Path.Combine("~/SavedFolder/PDF", txtName.Text));
        Response.TransmitFile(filePath);
        Response.End();
    }