如何从文件中下载PDF文件,点击按钮
本文关键字:文件 按钮 PDF 下载 | 更新日期: 2023-09-27 18:10:07
我想下载一个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?
试试这个
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();
}