Dim fName1 As String = e.Item.Cells(1).Text.Trim  '文件名
        Dim fName As String
        '****************文件下载***********************************************
        Try
            fName = viewstate("curDirectory") + fName1   ‘全路径文件名
            Dim fInfo As System.IO.FileInfo = New FileInfo(fName)
            Response.Clear()
            Response.ClearContent()
            Response.ClearHeaders()
            Response.Buffer = True
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fName1, System.Text.Encoding.UTF8))
            Response.AddHeader("Content-Length", fInfo.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Dim MyFileStream As FileStream = New FileStream(fName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
            Dim FileSize As Long = MyFileStream.Length
            Dim Buffer(CInt(FileSize)) As Byte
            MyFileStream.Read(Buffer, 0, Int32.Parse(FileSize))
            MyFileStream.Close()
            Response.BinaryWrite(Buffer)
         Catch ex As Exception
            Page.RegisterStartupScript("look", "<script language='javascript'>window.alert('文件不存在!' )</script>")
            Exit Sub
        Finally
            Response.End()
        End Try

解决方案 »

  1.   

    <%@ Page Language="VB" Debug="true"%>
    <%@ Import NameSpace="System.IO" %>
    <%@ Import NameSpace="System.Net" %>
    <script runat="server">
    Sub Button1_Click(s As Object,e As EventArgs)
        DownloadFile("c:\rt.gif",true)
    End SubSub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
      Dim path As Path
      Dim fullpath = path.GetFullPath(fname)
      Dim name = path.GetFileName(fullpath)
      Dim ext = path.GetExtension(fullpath)
      Dim type As String = ""
      If Not IsDBNull(ext) Then
          ext = LCase(ext)
      End If
      Select Case ext
          Case ".htm", ".html"
              type = "text/HTML"
          Case ".txt"
              type = "text/plain"
          Case ".doc", ".rtf"
              type = "Application/msword"
          Case ".csv", ".xls"
              type = "Application/x-msexcel"
          Case Else
              type = "text/plain"
      End Select
      If (forceDownload) Then
          Response.AppendHeader("content-disposition", _
          "attachment; filename=" + name)
      End If
      If type <> "" Then
          Response.ContentType = type
      End If
      Response.WriteFile(fullpath)
      Response.End()
    End Sub
    </script>
    <form runat="server">
    <asp:Button id="button1" onclick="Button1_Click" runat="server" Text="DownLoadFile"/>
    </form>