htmlCode :
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" CssClass="gridview" EmptyDataText="暂无产品信息"
Width="99%" RowStyle-HorizontalAlign="center" OnDataBound="gvData_DataBound" OnRowCommand="gvData_RowCommand" >
<RowStyle Height="23px" />
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<%#Container.DataItemIndex + 1 %>
</ItemTemplate>
<ItemStyle Width="30px" />
</asp:TemplateField>
<asp:BoundField DataField="supplier_name" HeaderText="供应商" />
<asp:BoundField DataField="file_name" HeaderText="文件名" />
<asp:BoundField DataField="file_type" HeaderText="文件类型" />
<asp:BoundField DataField="update_date" HeaderText="日期" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<img src="../images/gif-0068.gif"/> <asp:LinkButton ID="lnkbtnDown" CssClass="lnkbtn" runat="server" CommandName="DOWN" OnClick="lnkbtndown_Click" CommandArgument='<%#Eval("filename") %>'>下载</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Center" />
<EmptyDataRowStyle ForeColor="Red" HorizontalAlign="Center" />
</asp:GridView>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Loading();
}
}
private void Loading()
{
//file_type(文件类型字段1表示文件放在reportfiles文件夹,2表示文件放在yieldratereportfile文件夹)file_name(文件名字段)
string strSql = "select A.ID,supplier_name,file_name,A.update_man,update_date,file_type,file_type+'#'+file_name as filename from supplier_file A,sqe_supplierinfo B where A.supplier_id = B.Supplier_ID ";
DataTable dt = fun.GetDataTable(strSql);
gvData.DataSource = dt;
gvData.DataBind();
dt.Dispose();
}
protected void lnkbtndown_Click(object sender, EventArgs e)
{
string strFullName = (sender as LinkButton).CommandArgument;
string[] strName = strFullName.Split('#');
if (strName[0].ToString() == "1")//此处分两个文件夹
{
FileDownload(System.Web.HttpContext.Current.Server.MapPath("../") + "reportfiles\\" + strName[1]);
}
else
{
FileDownload(System.Web.HttpContext.Current.Server.MapPath("../") + "yieldratereportfiles\\" + strName[1]);
}
}
public void FileDownload(string strFullName)//下载文件绝对路径
{
if (File.Exists(strFullName))
{
FileInfo DownloadFile = new FileInfo(strFullName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);//DownloadFile.FullName保存文件名
Response.Flush();
Response.End();
}
else
{
Response.Write("<script>alert('文件不存在');</script>");
}
}