ITPub博客

首页 > 应用开发 > IT综合 > 下载附件代码(VB.NET)

下载附件代码(VB.NET)

原创 IT综合 作者:xing2999 时间:2011-11-21 19:16:16 0 删除 编辑

存储附件的表LONION_FUJ,表结构如下:

NAId      ----->附件ID

NAFileName   ----->附件名

NAFilePath   ----->附件路径


点击某个按钮或触发某个事件下载附件,代码如下:

[@more@]

Protected Sub ViewButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  Dim i As Integer = DataList1.EditItemIndex

  Dim label As Label = (CType(DataList1.Items(i).FindControl("m_id"), Label))

  Dim strSQL As String = "SELECT * FROM LONION_FUJ WHERE NOTICEID = " + label.Text

  Try

   If Not CargoBookingDBConnect.CargoBookingDBConnect() Then

   End If

   SqlDA = New SqlDataAdapter(strSQL, strConnStringCargoBookingDB)

   SqlDA.Fill(sDataSet)

   Dim dt As DataTable = sDataSet.Tables(0)

   Dim filePath As String = dt.Rows(0)("NAFilePath")

   Dim fileInfo As New IO.FileInfo(filePath)

   Response.Clear()

   Response.ClearContent()

   Response.ClearHeaders()

   Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name))

   Response.AddHeader("Content-Length", fileInfo.Length.ToString())

   Response.AddHeader("Content-Transfer-Encoding", "binary")

   Response.ContentType = "application/octet-stream"

   Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312")

   Response.WriteFile(fileInfo.FullName)

   Response.Flush()

   Response.End()

  Catch ex As Exception

    Throw ex

   End Try

End Sub


PS:Server.UrlEncode(),引用此方法使得下载的文件名称如果是中文的话不会显示乱码

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24389441/viewspace-1056440/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-09-29

  • 博文量
    42
  • 访问量
    148196