心若浮云

认真做好每一件事,开心就好。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用LinkButton实现文件下载的功能

1、单个文件下载功能的实现
在aspx页面中的代码如下:
<asp:linkbutton id="LBtnDown" runat="server">Names.xls</asp:linkbutton>

在aspx.vb后台程序中的代码如下:
Private Sub LBtnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LBtnDown.Click

 '下载的文件名(包括路径)
        Dim FileName As String = Server.MapPath("") & "\" & LBtnDown.Text        
        Dim fileStream As New FileStream(FileName, FileMode.Open)
        Dim fileSize As Long = fileStream.Length
        Dim inta As Integer = CInt(fileSize)
       '客户端下载时显示的文件名
        Dim OutPutFileName As String = "Outputfile.xls"

        Context.Response.ContentType = "application/octet-stream"
 '注意后面必须后System.Text.Encoding.UTF8进行编码
        Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & HttpUtility.UrlEncode(OutPutFileName, System.Text.Encoding.UTF8))
        Context.Response.AddHeader("Content-Length", fileSize.ToString())
        Dim fileBuffer(inta) As Byte
        fileStream.Read(fileBuffer, 0, inta)
        fileStream.Close()
        Context.Response.BinaryWrite(fileBuffer)
        Context.Response.End()

End Sub

2、多个文件下载功能的实现(在DataGrid使用LinkButton)
在aspx页面中的代码如下:
<asp:datagrid id="UserInfoList" runat="server" CellPadding="0" CellSpacing="1" Width="90%" HorizontalAlign="Center" BackColor="#E1E1E1" BorderWidth="0px" AutoGenerateColumns="False" DataKeyField="UserID" AllowPaging="True" OnPageIndexChanged="page_change">
 <AlternatingItemStyle HorizontalAlign="Center" Height="30px" VerticalAlign="Middle" BackColor="#EEF1F7"></AlternatingItemStyle>
 <ItemStyle HorizontalAlign="Center" Height="30px" VerticalAlign="Middle" BackColor="#F8F8F8"></ItemStyle>
 <HeaderStyle Font-Bold="True" HorizontalAlign="Center" Height="30px" ForeColor="#000099" BackColor="#FFFFEE"></HeaderStyle>
 <Columns>
  <asp:TemplateColumn HeaderText="LoginName">
   <HeaderStyle Width="20%"></HeaderStyle>
   <ItemStyle HorizontalAlign="Center" Height="30px"></ItemStyle>
   <ItemTemplate>
    <%# databinder.eval(container.dataitem,"LoginName")%>
   </ItemTemplate>
  </asp:TemplateColumn>
  '隐藏列,用来保存下载的文件名
  <asp:BoundColumn Visible="False" DataField="FileName" ReadOnly="True"></asp:BoundColumn>
  <asp:TemplateColumn HeaderText="LoginPassword">
   <HeaderStyle Width="20%"></HeaderStyle>
   <ItemStyle HorizontalAlign="Center"></ItemStyle>
   <ItemTemplate>
    <%# databinder.eval(container.dataitem,"LoginPassword")%>
   </ItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="UserName">
   <HeaderStyle Width="20%"></HeaderStyle>
   <ItemStyle HorizontalAlign="Center"></ItemStyle>
   <ItemTemplate>
    <%# databinder.eval(container.dataitem,"UserName")%>
   </ItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn HeaderText="FileDownload">
   <HeaderStyle Width="20%"></HeaderStyle>
   <ItemStyle HorizontalAlign="Center"></ItemStyle>
   <ItemTemplate>
    <asp:LinkButton id="LBtnDown" runat="server" EnableViewState="True" CommandName="FileDownload" ToolTip="Click to download file">Download the file</asp:LinkButton>
   </ItemTemplate>
  </asp:TemplateColumn>
 </Columns>
 <PagerStyle Height="30px" Font-Bold="True" HorizontalAlign="Right" BackColor="#FFFFEE" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

注意这里使用到CommandName="FileDownload",这个在后台程序中会用到。

在aspx.vb后台程序中的主要代码如下:
注意在DataGrid中使用ItemCommand事件实现此功能

''' -----------------------------------------------------------------------------
''' <summary>
''' 点击每行的download the file链接,实现文件下载
''' </summary>
''' <param name="source"></param>
''' <param name="e"></param>
''' <remarks>
''' </remarks>
''' <history>
'''  [nijun] 2006-1-13 Created
''' </history>
''' -----------------------------------------------------------------------------
Private Sub UserInfoList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles UserInfoList.ItemCommand

 Dim FileName As String
        Dim fileStream As FileStream
        Dim fileSize As Long
        Dim inta As Integer
        Dim OutPutFileName As String

        '判断是点击download the file时才触发下载文件程序
        If (e.CommandName = "FileDownload") Then
            '下载的文件名(包括路径),e.Item.Cells(1).Text.Trim获得文件名(隐藏列实现)
            FileName = Server.MapPath("") & "\" & e.Item.Cells(1).Text.Trim
            fileStream = New FileStream(FileName, FileMode.Open)
            fileSize = fileStream.Length
            inta = CInt(fileSize)
            '客户端下载时显示的文件名
            OutPutFileName = "Outputfile.xls"

            Context.Response.ContentType = "application/octet-stream"
            '注意后面必须后System.Text.Encoding.UTF8进行编码
            Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & HttpUtility.UrlEncode(OutPutFileName, System.Text.Encoding.UTF8))
            Context.Response.AddHeader("Content-Length", fileSize.ToString())
            Dim fileBuffer(inta) As Byte
            fileStream.Read(fileBuffer, 0, inta)
            fileStream.Close()
            Context.Response.BinaryWrite(fileBuffer)
            Context.Response.End()
        End If
End Sub


程序文件的下载地址:https://files.cnblogs.com/tawny/NETExample20060113.rar
将rar中的目录和文件copy到NETExample目录下,在Microsoft Visual Studio .NET 2003编辑器中将目录和文件包括到解决方案中,然后就可以运行了。

注意:
如在程序运行过程中,点击链接下载文件时系统报错,访问文件被拒绝。请对FileDownload目录下的names.xls文件添加ASPNET用户的读写权限。
具体操作步骤为:选中names.xls文件,右键属性—〉安全,在安全中添加ASPNET用户,然后给ASPNET用户添加读写权限。

posted on 2006-01-13 16:12  心若浮云  阅读(1965)  评论(0编辑  收藏  举报