简体   繁体   English

如何从单击按钮的文件中下载PDF文件?

[英]How to download PDF file from files on button click?

I want to dowlnload a PDF file on a button click. 我想在单击按钮时加载PDF文件。 I've try like that: 我已经尝试过:

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

    }

But nothing happens, I mean when I debug it there is no exception, but the file is not downloading. 但是没有任何反应,我的意思是,当我调试它时,没有异常,但是文件没有下载。

Does anyone knows where my mistake is, and what I should do to download the PDF? 有谁知道我的错误在哪里,以及我应该怎么下载PDF?

Try this 尝试这个

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

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

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