标签:addheader data link div title 自定义 控件 page private
string DirectoryPath = Server.MapPath("Upload/");
string[] filePath = Directory.getFiles(DirectoryPath);
当然展现给用户的可能不仅仅只是文件名称,有时候可能要向用户显示文件类型、文件大小、上传时间等。这时候就可以把文件封装成一个类了。目录下肯定是有一个或多个文件的,所以用集合来保存。然后这个集合中就有了下载文件的所有信息了。接下来就显示给用户。页面显示可以用Repeater控件
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName));
Response.TransmitFile(filePath);
Response.Flush();
Response.End();
代码:
1.封装的存放文件信息的类:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; /// <summary> /// 封装文件类 /// </summary> public class FilesInfo { private string filePath; private string fileName; private string size; private string time; public string FileName {get { return fileName; } set { fileName = value; } } public string Size { get { return size; } set { size = value; } } public string Time { get { return time; } set { time = value; } } public string FilePath { get { return filePath; } set { filePath = value; } } public FilesInfo(string filePath) { if (filePath != null) { FileInfo info = new FileInfo(filePath); this.FilePath = filePath; FileName = Path.GetFileName(FilePath); Size = (info.Length / 1024).ToString("0.00"); Time = info.CreationTime.ToString("yyyy/MM/dd hh:mm:ss"); } } }
2.前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> #tableCss { border:1px solid red; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> <HeaderTemplate> <table id="Table1" class="tableCss"> <tr> <td> 文件名称 </td> <td> 文件大小 </td> <td> 文件创建时间 </td> <td> 文件操作 </td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="Label1" runat="server" Text=‘<% #Eval("文件名称") %>‘></asp:Label> </td> <td> <asp:Label ID="Label2" runat="server" Text=‘<%#Eval("文件大小") %>‘></asp:Label> </td> <td> <asp:Label ID="Label3" runat="server" Text=‘<%#Eval("文件创建时间") %>‘></asp:Label> </td> <td> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument=‘<%#Eval("link") %>‘>下载文件</asp:LinkButton> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { BindingData(GetFileNames()); } /// <summary> /// 获取目录下的文件 /// </summary> /// <returns>存放文件信息的集合</returns> public List<FilesInfo> GetFileNames() { string[] filePath = Directory.GetFiles(@"H:\c#练习"); List<FilesInfo> FilesInfos = new List<FilesInfo>(); foreach (string fileNames in filePath) { FilesInfo info = new FilesInfo(fileNames); FilesInfos.Add(info); } return FilesInfos; } /// <summary> /// 给控件绑定数据 /// </summary> /// <param name="info"></param> public void BindingData(List<FilesInfo> info) { DataTable dt = new DataTable(); dt.Columns.Add("文件名称"); dt.Columns.Add("文件大小"); dt.Columns.Add("文件创建时间"); dt.Columns.Add("link"); foreach (FilesInfo files in info) { DataRow row = dt.NewRow(); row[0] = files.FileName; row[1] = files.Size; row[2] = files.Time; row[3] = files.FilePath; dt.Rows.Add(row); } Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { string filePath = e.CommandArgument.ToString(); string fileName = Path.GetFileNameWithoutExtension(filePath); Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName)); Response.TransmitFile(filePath); Response.Flush(); Response.End(); } }
标签:addheader data link div title 自定义 控件 page private
原文地址:http://www.cnblogs.com/sjyzz/p/7655196.html