Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download a file from a database

Status
Not open for further replies.

kazz01

Technical User
Joined
Oct 16, 2003
Messages
11
Location
GB
Hi there

I am in the process of building a small "Content Management System" where users will beable to Upload files, (mainly PDF's) to an access database on my website. The users will also beable to search for files and then DOWNLOAD their selected file from the database. Ive got the upload and search pages build, but can't seem to figure out the download bit.

The search page displays the Files in a table, with a link to the file in one of the columns.
Ive used the following piece of code to make the link:

<A href=&quot;download.asp?FileName=<%= rstSearch.Fields(&quot;FileName&quot;).Value %>&quot;><%= rstSearch.Fields(&quot;FileName&quot;).Value %></a>

The page download.asp contains the following code:


<%
Dim oConn
Dim oRs
Dim sSQL
Dim cnnSearch
Dim rstSearch


set oConn = server.createobject(&quot;adodb.connection&quot;)
set oRs = server.createobject(&quot;adodb.recordset&quot;)
oConn.Open = (&quot;DSN=seibDSN&quot;)
sSQL = &quot;SELECT * FROM Files WHERE FileName = &quot; & Request.QueryString(&quot;FileName&quot;)

Filename = &quot;&quot;
Response.ContentType = &quot;application/octet-stream&quot;
Response.AddHeader &quot;Content-Disposition&quot;, &quot;attachment; FileName = &quot; & FileName

Response.AddHeader &quot;Document&quot;, FileName
Response.AddHeader &quot;Script_Name&quot;, FileName

Response.Write Head
content = StatsRS.GetString(,,&quot;,&quot;,vbcrlf,&quot;&quot;)
response.write content
Response.Flush


Response.End

%>

All this, but the files will not download. Can anyone help?
Thanks
 
I've had success doing it like this:

set rs = conn.execute(strSQL)
If Not Rs.EOF Then
response.ContentType=&quot;application/asp-unknown&quot;
response.AddHeader &quot;content-disposition&quot;,&quot;attachment; filename=&quot; & Rs(0) ' the file name
Response.ContentType = Rs(1) 'The content type
Response.BinaryWrite Rs(2) 'The actual file

Else
Response.Write(&quot;File could not be found&quot;)
End If
 
Not shure if those to headers
Response.AddHeader &quot;Document&quot;, FileName
Response.AddHeader &quot;Script_Name&quot;, FileName
will have to be there.
How looks your FILES table structure?
Check if the data dumped to the database is corect. specially the file content.


________
George, M
 
Veep

I have tried to implement your piece of code but i get the following error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

/download.asp, line 27

Im not sure exactly what this means, as im quite new to ASP

Any suggestions?
Thanks
 
That usually has something to do with your SQL statement. Let's see the code.
 
Oh, sorry I forgot

Line 27 is:

set oRs = oConn.execute(strSQL)

 
here's the code for the whole page download.asp

<%
Dim oConn
Dim oRs
Dim sSQL
Dim cnnSearch
Dim rstSearch
Dim strSQL

set oConn = server.createobject(&quot;adodb.connection&quot;)
set oRs = server.createobject(&quot;adodb.recordset&quot;)
oConn.Open = (&quot;DSN=seibDSN&quot;)
strSQL = &quot;SELECT FileName, FileID, ContentType, BinaryData FROM Files WHERE FileName = &quot; & Request.QueryString(&quot;FileName&quot;)
set oRs = oConn.execute(strSQL)
if Not oRs.EOF Then
Response.ContentType=&quot;application/asp-unknown&quot;
Response.AddHeader &quot;content-disposition&quot;,&quot;attachment; FileName=&quot; & oRs(0) ' the file name
Response.ContentType = Rs(1) 'The content type
Response.BinaryWrite Rs(2) 'The actual file

Else
Response.Write(&quot;File could not be found&quot;)
End If

Response.End

%>
 
Try this:
FileName=&quot; & oRs(&quot;FileName&quot;) ' the file name
Response.ContentType =oRs(&quot;ContentType&quot;) 'The content type
Response.BinaryWrite oRs(&quot;BinaryData&quot;) 'The actual file


FileName, FileID, ContentType, BinaryData
These are all real columns in your Files table, aren't they?
 
Yep, FileName, FileID, ContentType, BinaryData are
the columns in my database.

I tried that amend, and now I get the following error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '* WHERE FileName = SEI234_rev00.pdf'.

/download.asp, line 27
 
strSQL = &quot;SELECT FileName, FileID, ContentType, BinaryData FROM Files WHERE FileName ='&quot; & Request.QueryString(&quot;FileName&quot;) & &quot;'&quot;

It's your SQL Statement. I added some quotes.
 
BTW. I always assign stuff to variables before I use them in a SQL statement:
myFileName=request.querystring(&quot;FileName&quot;)
strSQL = &quot;SELECT FileName, FileID, ContentType, BinaryData FROM Files WHERE FileName ='&quot; & myFileName & &quot;'&quot;

Makes it a little easier to see all of your quotes.
 
Well, its all working now.
Thanks to all who contributed their brains to this forum.
Especially Veep, Thank You

Kazz01
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top