download.aspx.vb

来自「是可以运行的电子光盘 有程序与PPT介绍 对于学习VB。NET的有参考意义」· VB 代码 · 共 53 行

VB
53
字号
Imports System.io

Partial Class FileManager_Download
    Inherits WebDiskBasePage
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        'Session("downpass")会话的目的是控制在通过密码访问的时候,密码错误将设置Session("downpass")=False
        If Convert.ToBoolean(Session("downpass")) Then
            ' Retrieve the path of the file to download, and create a 
            ' FileInfo object to read its properties
            Dim thePath As String = Server.UrlDecode(Request.Params("File"))

            '确定文件是在服务目录下,因为其他共享文件也需要下载
            If thePath.StartsWith(Session("ServerDir")) Then
                '判断文件是否存在
                If File.Exists(thePath) Then
                    Dim theFile As System.IO.FileInfo = New System.IO.FileInfo(thePath)

                    ' Clear the current output content from the buffer
                    Response.Clear()

                    ' Add the header that specifies the default filename 
                    ' for the Download/SaveAs dialog
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & Server.UrlEncode(theFile.Name))
                    ' Add the header that specifies the file size, so that the 
                    ' browser can show the download progress
                    '//  Response.AddHeader("Content-Length", theFile.Length.ToString())

                    ' Specify that the response is a stream that cannot be read _
                    ' by the client and must be downloaded
                    '//  Response.ContentType = "application/octet-stream"

                    ' Send the file stream to the client

                    'this is sample
                    Response.AddHeader("Content-Length", theFile.Length.ToString())
                    Response.ContentType = "application/octet-stream"
                    Response.WriteFile(Server.UrlDecode(theFile.FullName))

                    ' Stop the execution of this page
                    Response.End()
                Else
                    Response.Write("抱歉,该文件不存在!")
                    Response.End()
                End If
            End If
        End If
    End Sub

End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?