downloading files from a sql database

Webfuzions

New member
Joined
Oct 25, 2006
Messages
4
Programming Experience
Beginner
I have the path to the file stored in my sql database. Now I want to download that file(.jpg,.doc. etc) from the database and open a download dialog window...does anyone have a way to do this in the simplest form.:confused:

ok i made some progress but i'm not getting the right path to the file that is stored in the database. I'm getting this now

"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\drummer"

instead of

C:\WebSites\BDCGroup\jobs\Damian\21\record_companies.txt
 
Last edited:
How are we supposed to know what you're doing wrong if we don't know what you're doing. Would you ask a mechanic to fix your car without seeing the engine? Post your code. Not all of it. Just the relevant section.
 
the code:

VB.NET:
Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim cmd As New SqlCommand
Dim conn As New SqlConnection
Dim dr As SqlDataReader
Dim sSql As String
sSql = "Select pathtofile from Jobs Where pathtofile = '" & Session("filename") & "'"
Try
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.ConnectionString = Globals.GetConnectionString()
conn.Open()
With cmd
'make a connection
.Connection = conn
.Parameters.Add(New SqlParameter("@pathtofile", SqlDbType.Text)).Value = fname
.CommandType = CommandType.Text '.Text 
.CommandText = sSql 'run sSql
 
'execute the data reader
dr = .ExecuteReader()
End With
Catch ex As Exception
MsgBox("Problems downloading file!!")
Finally
'dr.Close()
conn.Close()
End Try
'use the variable passed in the sub
Dim fullpath As String
fullpath = Path.GetFullPath(fname)
' dont change nothing below this line
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
 
Protected Sub uwgDownloadGrid_DblClick(ByVal sender As Object, _
ByVal e As Infragistics.WebUI.UltraWebGrid.ClickEventArgs) Handles uwgDownloadGrid.DblClick
Session("filename") = uwgDownloadGrid.Rows(uwgDownloadGrid.DisplayLayout.ActiveRow.Index).Cells(0).Value
Dim filename As String = Session("filename")
'Session("username") = e.Row.Cells(0).Text
DownloadFile(filename, True)
End Sub
 
Last edited by a moderator:
Problem resolved!!!

the code:

VB.NET:
Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim cmd As New SqlCommand
Dim conn As New SqlConnection
Dim dr As SqlDataReader
Dim sSql As String
sSql = "Select pathtofile from Jobs Where pathtofile = '" & Session("filename") & "'"
Try
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.ConnectionString = Globals.GetConnectionString()
conn.Open()
With cmd
'make a connection
.Connection = conn
.Parameters.Add(New SqlParameter("@pathtofile", SqlDbType.Text)).Value = fname
.CommandType = CommandType.Text '.Text 
.CommandText = sSql 'run sSql
 
'execute the data reader
dr = .ExecuteReader()
End With
Catch ex As Exception
MsgBox("Problems downloading file!!")
Finally
'dr.Close()
conn.Close()
End Try
'use the variable passed in the sub
Dim fullpath As String
fullpath = Path.GetFullPath(fname)
' dont change nothing below this line
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
 
Protected Sub uwgDownloadGrid_DblClick(ByVal sender As Object, _
ByVal e As Infragistics.WebUI.UltraWebGrid.ClickEventArgs) Handles uwgDownloadGrid.DblClick
Session("filename") = uwgDownloadGrid.Rows(uwgDownloadGrid.DisplayLayout.ActiveRow.Index).Cells(0).Value
Dim filename As String = Session("filename")
'Session("username") = e.Row.Cells(0).Text
DownloadFile(filename, True)
End Sub

Im not sure if anyone out there was familiar with the infrigistics webgrid..but I think I finally resolved my problem by editing one line

cells(0).value needed to be simply

cells(1).value

now I'm getting the proper value.

sorry about earlier not posting the code.
 
Back
Top