Asp.net通过获取数据库中的文件路径怎么实现下载?

Asp.net通过获取数据库中的文件路径怎么实现下载?,第1张

数据库里存文件名,视频播放路径写上文件夹所在路径加上数据库存的文件名。

后台写:

public

string

url

url

=

sqlhelper.executescalar(sqlhelper.sqlconnectionstring,

commandtype.text,

"select

vc_videourl

from

tb_video

where

i_videoid="+request["id"],null).tostring()

前台按钮单击事件BtnExcel_Click2,应该看的懂吧?,自己加上错误处理机制就行了

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

'sqlstrShare 全局查询的SQL语句,导出所有部门备品

Dim sqlstrShare As String = “SQL查询语句”

Dim sqlconn As New SqlConnection

Dim cmdString As String = sqlstrShare

sqlconn.ConnectionString = connclass.ConnStr

Dim Da As SqlDataAdapter = New SqlDataAdapter(cmdString, sqlconn)

Dim ds As System.Data.DataSet = New Data.DataSet()

Da.Fill(ds, "Data")

DataToExcel(ds, Me.Response, "SPARTREPORT")

ds.Dispose()

Da.Dispose()

End Sub

Sub DataToExcel(ByVal ds As DataSet, ByVal response As HttpResponse, ByVal fileName As String)

Dim str As String

Dim dt As System.Data.DataTable

dt = ds.Tables(0)

For i As Integer = 0 To dt.Columns.Count - 1

dt.Columns.Item(i).ColumnName = connclass.ReplaceStr(dt.Columns.Item(i).ColumnName)

Next

str = Format(Now(), "_yyyyMMdd") &"_" &Now.ToString("HHmmss")

fileName = fileName + str

response.Clear()

Dim myGrid As GridView = New GridView

myGrid.DataSource = dt

myGrid.DataBind()

HttpContext.Current.Response.Charset = "gb2312"

Dim oSW As System.IO.StringWriter = New System.IO.StringWriter()

Dim oHW As HtmlTextWriter = New HtmlTextWriter(oSW)

myGrid.RenderControl(oHW)

response.Write(oSW.ToString())

response.ContentType = "application/ms-excel"

response.AppendHeader("Content-Disposition", "attachmentfilename=" + fileName + ".xls")

response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")

response.Flush()

response.Close()

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6773650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存