MSDN 杂志
MSDN 主页 > MSDN 技术资源库 >  Web 下载:将更智能的 ASP.NET 文件下载体验内置到您的 Web 应用程序中
图 2 设置 Content-Disposition 响应标头
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Response.Clear()
        Response.AddHeader("Content-Disposition", _
                           "attachment; filename=" & toDownload.Name)
        Response.AddHeader("Content-Length", _
                           file.Length.ToString())
        Response.ContentType = "application/octet-stream"
        Response.WriteFile(file.FullName)
        Response.End()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

图 4 将文件块写入客户端
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Const ChunkSize As Long = 10000
        Dim buffer(ChunkSize) As Byte
            
        Response.Clear()
        Using iStream As FileStream = File.OpenRead(path)
            Dim dataLengthToRead As Long = iStream.Length
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("Content-Disposition", _
                               "attachment; filename=" & toDownload.Name)
            While dataLengthToRead > 0 AndAlso Response.IsClientConnected
                Dim lengthRead As Integer = _
                    iStream.Read(buffer, 0, ChunkSize)
                Response.OutputStream.Write(buffer, 0, lengthRead)
                Response.Flush()
                dataLengthToRead = dataLengthToRead - lengthRead
            End While
        End Using
        Response.Close()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

图 5 使用 TransmitFile
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dlDir As String = "downloadfiles/"
    Dim strFileName As String = Request.QueryString("FileName")
    Dim path As String = Server.MapPath( _
        dlDir + Request.QueryString("FileName"))
    Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(path)

    If IsSafeFileName(strFileName) AndAlso toDownload.Exists Then
        Response.Clear()
        Select Case System.IO.Path.GetExtension(strFileName)
            Case ".zip"
                Response.ContentType = "application/x-zip-compressed"
                Response.AddHeader("Content-Disposition", _
                    "attachment;filename=NEWDL_" + toDownload.Name)
                Response.TransmitFile(path)

            Case Else
               ‘ 文件扩展名不受支持。
        End Select
        Response.End()
    Else
        BindFileDataToGrid("Name")
    End If
End Sub

 

© 2006 Microsoft Corporation 版权所有。保留所有权利。使用规定。