后台代码:
public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protected void Page_Load(object sender, EventArgs e) { LoadData(); } protected void BtnSave_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string name = FileUpload1.FileName; string filepath = Server.MapPath("~/") + "upload\" + name; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = string.Format("insert into PathTable (Name,Path) values('{0}','{1}')",name,filepath); int count = comm.ExecuteNonQuery(); if (count > 0) { FileUpload1.SaveAs(filepath); LoadData(); Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功')</script>"); } else { ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('保存失败')</script>"); } } } } public void LoadData() { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select * from PathTable"; comm.ExecuteScalar(); SqlDataAdapter adapter = new SqlDataAdapter(comm); DataSet ds = new DataSet(); adapter.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } } public static void DownloadFile(string physicalFilePath) { FileStream stream = null; try { stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); int bufSize = (int)stream.Length; byte[] buf = new byte[bufSize]; int bytesRead = stream.Read(buf, 0, bufSize); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(physicalFilePath)); HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead); HttpContext.Current.Response.End(); } finally { stream.Close(); } } protected void LinkButton_Click(object sender, CommandEventArgs e) { if (e.CommandArgument!=null) { string path = e.CommandArgument.ToString(); DownloadFile(path); } }
前台代码:
<head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="编号"> <ItemTemplate> <asp:Label ID="Id" Text='<%#Eval("Id") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="文件名"> <ItemTemplate> <asp:Label ID="Name" Text='<%#Eval("Name") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="路径"> <ItemTemplate> <asp:Label ID="Path" Text='<%#Eval("Path") %>' runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="下载"> <ItemTemplate> <asp:LinkButton Text="下载" runat="server" CommandArgument='<%#Eval("Path")%>' OnCommand="LinkButton_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <p> <asp:FileUpload ID="FileUpload1" runat="server"/> </p> <p> <asp:Button ID="BtnSave" runat="server" Text="保存" OnClick="BtnSave_Click" /> </p> </form> </body>
注意:
1、GridView列中的TemplateField属性很实用,可以在其中添加其他发服务器控件、绑定事件和进行字段转换等;
2、设置CommandArgumet和OnCommand事件,可以在后台方便的获取绑定的字段;
3、Text='<%#Eval("Id")%>' 单引号内放双引号。