C# 从目录列表下载PDF文件

C# 从目录列表下载PDF文件,c#,asp.net,gridview,webforms,C#,Asp.net,Gridview,Webforms,我使用Gridview控件在asp.net webforms中显示目录的内容 内容将被过滤以仅显示PDF文件 我在TemplateField中还有一个按钮。点击按钮,用户应该能够下载并保存PDF文件 Gridview中显示的列包括文件名、修改日期和大小 如何编程单击按钮以下载和保存PDF文件?我有一个执行文件下载的功能 public static void DownloadFile(string FilePath, System.Web.HttpResponse response) {

我使用Gridview控件在asp.net webforms中显示目录的内容

内容将被过滤以仅显示PDF文件

我在TemplateField中还有一个按钮。点击按钮,用户应该能够下载并保存PDF文件

Gridview中显示的列包括文件名、修改日期和大小


如何编程单击按钮以下载和保存PDF文件?

我有一个执行文件下载的功能

public static void DownloadFile(string FilePath, System.Web.HttpResponse response)
{
    System.IO.FileInfo file = new System.IO.FileInfo(FilePath);
    if ((file.Exists))
    {
        response.Clear();
        response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        response.AddHeader("Content-Length", file.Length.ToString());
        response.ContentType = "application/octet-stream";
        response.WriteFile(file.FullName);
        response.End();
        response.Close();
        file = null;
    }
}

FilePath
参数是物理路径,因此如果您有虚拟路径(例如
~/Folder/file.pdf
),可能需要使用
Server.MapPath(…)
函数调用该函数

按钮中
单击事件,编写以下代码

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Response.ContentType = "Application/pdf"; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=Your_Pdf_File.pdf"); 
    Response.TransmitFile(Server.MapPath("~/Files/Your_Pdf_File.pdf"));
    Response.End(); 
} 

请发布您的gridview标记和文件保存位置谢谢您的建议。如何从gridview获取文件名?我使用Scott的这篇文章作为起点。你能发布GridView的代码和相关代码吗?没有这些,回答起来就不容易。。