频道栏目
首页 > 程序开发 > Web开发 > ASP.Net > 正文
ASP.NET中实现直接从网页上下载文件,而不须引用文件URL来下载
2006-07-14 07:00:22           
收藏   我要投稿
函数名:ResponseFile
功能  :客户端从服务器端下载一个文件
返回值:返回True表示服务器响应成功,返回False表示失败
参数  :
        PageResponse       响应客户端的Response对象,用Page.Response引用
        DownloadFileName   客户端下载文件的文件名
        LocalFilePath      服务器端待下载文件的路径
        DownloadBuffer     服务器端读取文件的缓冲区大小,单位为KB
Public Function ResponseFile(ByRef PageResponse As HttpResponse, ByVal DownloadFileName As String, ByVal LocalFilePath As String, ByVal DownloadBuffer As Long) As Boolean
        Dim Reader As System.IO.FileStream
        Dim Buffer() As Byte
        Dim FileLength As Long
        Dim FileBuffer As Long = 1024 * DownloadBuffer
        Dim ReadCount As Long
        ReadCount = FileBuffer
        ReDim Buffer(ReadCount - 1)
        Try
                Reader = System.IO.File.OpenRead(LocalFilePath)
                FileLength = Reader.Length
                Try
                        PageResponse.Buffer = False
                        PageResponse.AddHeader("Connection", "Keep-Alive")
                        PageResponse.ContentType = "application/octet-stream"
                        PageResponse.AddHeader("Content-Disposition", "attachment;filename=" + DownloadFileName)
                        PageResponse.AddHeader("Content-Length", FileLength.ToString)
                        While ReadCount = FileBuffer
                                ReadCount = Reader.Read(Buffer, 0, FileBuffer)
                                ReDim Preserve Buffer(ReadCount - 1)
                                PageResponse.BinaryWrite(Buffer)
                        End While
                        Response.End()
                Catch ex As Exception
                        Return False
                Finally
                        Reader.Close()
                End Try
        Catch ex As Exception
                Return False
        End Try
        Return True
End Function

点击复制链接 与好友分享!回本站首页
上一篇:ASP.NET2.0+SQL Server2005构建多层应用(1)
下一篇:.NET Framework Namespaces
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站