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!

File Upload to DB, and adding Fields

Status
Not open for further replies.

kazz01

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

I have build a small Content Management system, which allows users to upload files via an ASP page to an Access Database.
The Upload works fine, and the files are saved to the DB, which other users can then download.

The problem comes in where I would like the user to beable to add a "title" and "description" to the actual file that he uploads. I have tried various methods, including:

1. The page where the user selects the file to upload, has three fields Title, Description and File (Browse for file)
When this page is submitted I get the following error:

Request object error 'ASP 0207 : 80004005'
Cannot use Request.Form
/ToDatabase.asp, line 16
Cannot use Request.Form collection after calling BinaryRead.
-----------------------------------------------------------
2. If I remove the Title and Description fields from the page, and once the user has uploaded their file, the page then directs them to a second form, where they fill in the Title and Description. However, when i use this solution, the uploaded file is in a separate recordset, to the Title and Description.

What I need is for all the file data to be saved in the same recordset in the DB, so when a users searches for a file, the results come up in a table with a link to the physical file, then a Title and Description.

Can anyone HELP please?
Thanks
 
Do it in two steps. On the first pass through the form only show the Title and Description boxes. Submit the form to itself and assign these values to session variables and show the <input type=file>. Now you can change the action of the form to your normal processing page, write the file, title and description to the database and kill the session variables.
 
Veep

That is what I am trying to do, but it seems something is going wrong.

First, the user selects a file and uploads it, then on the following page, he/she would then enter the title & description. But it does not put the data into the same record set.
 
I would reverse the order. Make them enter a title and description first. Otherwise you could end up with them uploading the file and bailing out at the title and description screen. Besides that, if you assign the title and description to session variables before you insert the file, you can insert title, description and file at the same time. Otherwise you're going to need to grab the record ID from the inserted file and do another update to add the title and description. Hope I'm making sense.
 
Veep, thanks for your input & yes you are makeing sense even thought my own ASP knowledge is still limited.
What im not sure how to do is, make the 1st form (Title & Description) submit to itself, and howto assign those values to session variables?

I assume that somehow those variables would have to be passed to the next page where the user then selects the file to upload, and ALL the data is then only written to the DB?

If thats the case, would i not get the same error of :
-----------------------------------------------------------
Request object error 'ASP 0207 : 80004005'
Cannot use Request.Form
/ToDatabase.asp, line 16
Cannot use Request.Form collection after calling BinaryRead.
----------------------------------------------------------
because the Title and Description values come from a form?
 
Hi Veep

I have rebuild my pages as you described. First I gotta form, with &quot;Title&quot; & &quot;Description&quot;. This form assigns the two values to variables, which are passed to the &quot;File Select&quot; page.

On the &quot;File Select&quot; page, the values of those variables are displayed in the Text Boxes, and below that is the &quot;Browse For File&quot; textbox.

Once the file is selected, the page triggers the ToDatabase.asp page which should write all the data to the DB.
However on submitting the page the following error comes up:
-----------------------------------------------------------
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC Microsoft Access Driver]Error in row
/ToDatabase.asp, line 41
-----------------------------------------------------------
The code on the ToDatabase.asp page is:
-----------------------------------------------------------
30-sSQL = &quot;SELECT FileID, FileName, FileSize, ContentType, BinaryData, Title, Description FROM Files WHERE 1=2&quot;

31-oRs.Open sSQL, oConn, 3, 3
32-
33-oRs.AddNew
34-oRs.Fields(&quot;Title&quot;) = TitleText
35-oRs.Fields(&quot;Description&quot;) = DesText
36-oRs.Fields(&quot;FileName&quot;) = sFileName
37-oRs.Fields(&quot;FileSize&quot;) = oFile.Length
38-oRs.Fields(&quot;ContentType&quot;) = oFile.ContentType
39-oRs.Fields(&quot;BinaryData&quot;).AppendChunk = oFile.BinaryData & 40-ChrB(0)
41-oRs.Update
42-oRs.Close
------------------------------------------------------------
Please note that without the new variables (Title & Description) the ToDatabase file, uploads the file without problems.

Can anyone PLEASE help?
 
Here's an example of what I'm saying. This would be the first page. The user will enter the title and description and submit. After the form is submitted the title and description will be set to session variables and they will get to select their file.

<%
if request.Form(&quot;flag&quot;) <> &quot;&quot; then
flag=request.Form(&quot;flag&quot;)
Session(&quot;Title&quot;) = request.Form(&quot;Title&quot;)
Session(&quot;Description&quot;) = request.Form(&quot;Description&quot;)
Else
flag=0
End if
%>
<html><head></head>
<body>
<%IF flag=0 then%>
<form name=&quot;frm1&quot; method=&quot;post&quot; action=&quot;<%=request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot;>
<input type=&quot;hidden&quot; name=&quot;flag&quot; value=&quot;1&quot;>
Title: <input name=&quot;Title&quot; type=&quot;text&quot;>
<br>
Description: <input type=&quot;text&quot; name=&quot;Description&quot;>
<br>
<input type=&quot;submit&quot; name=&quot;btnSubmit&quot; value=&quot;Submit&quot;>
</form>
<%end if
If flag=1 then%>
<form name=&quot;frmFile&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;YourProcessingPage.asp&quot;>
Select File: <input type=&quot;file&quot; name=&quot;myFile&quot;>
<input type=&quot;submit&quot; name=&quot;btnSubmit&quot; value=&quot;Upload&quot;>
</form>
<%end if%>
</body>
</html>

Then on your processing page (I don't know what method you're using to insert the file but...) you would assign the title and description to variables like so:
<%
Title=session(&quot;Title&quot;)
Description=session(&quot;Description&quot;)
%>
Now you can add them to your database along with the file. Then once that process is complete:
Session(&quot;Title&quot;)=&quot;&quot;
Session(&quot;Description&quot;)=&quot;&quot;
 
I'm guessing that the error is because there are no values for title and description. When you're using enctype=&quot;multipart/form-data&quot; you can really only pass stuff around using the session. Trust me. I use that same upload routine all the time (I recognize the code (Lewis Moten I believe)) and it works fine.
 
Hey, I just noticed that you have criteria in your SQL which means that you're selecting an existing record. But in your recordset you are calling the &quot;AddNew&quot; method. If you are updating an existing record you need to comment out the line:
'oRs.AddNew
 
Hey veep

You're a genius, it works now with those Session Variables.
Thanks very much for help,its very much appreciated.

I owe you one.
Cheers
 
My pleasure. Make it a tall frosty one ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top