File upload problem (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


yogaboy -> File upload problem (11/14/2004 6:04:32)

I have a slight problem (ahem) but we won't go into that here[;)]

I have a page for file uploading (got the code from stardeveloper.com), and a form for the user to fill out giving some information about the document.

I want the user to fill out the info first and then upload the file. This requires 2 forms, because the fileupload needs an ENCTYPE="multipart/form-data". (I tried putting the forms together using that enctype and it didn't work)

Problem is, I need the filename to link with the info. If I get the filename from the info-form then I can't pass it to the upload-form because INPUT="file" doesn't have a VALUE attrib (no, it doesn't!).

Otherwise,
1. I get the user to browse to the document twice (giving the possibility of mistakes or horseplay, and a bit of user-frustration)
2. The user uploads before filling out the info, which gives the possibility of them uploading without filling out the info at all.

Anyone have any ideas how I do this????




Spooky -> RE: File upload problem (11/14/2004 13:15:21)

Typically uploaders have other methods for retrieving form values (each is different so youd need to check documentation)

For example, using Lewis Motens code, instead of request.form("FeildName") you use Upload.Fields("FeildName").Value




aaronwiles -> RE: File upload problem (11/15/2004 19:49:42)

quote:


I want the user to fill out the info first and then upload the file. This requires 2 forms, because the fileupload needs an ENCTYPE="multipart/form-data". (I tried putting the forms together using that enctype and it didn't work)


I know this can be done but I do not currently know how, although I will shortly be working on it...




aaronwiles -> RE: File upload problem (11/15/2004 20:00:33)

quote:

2. The user uploads before filling out the info, which gives the possibility of them uploading without filling out the info at all.


This is not ideal but I'm a little tired so mind is not working wonderfully...

As for step 2 you could always use two pages (make the process look like it is all one process but actually using two). first one saying.

------------------------------
PAGE 1

1. Name
2. Address
3. Post Code
4. Tel No
(etc.)

continue -->
------------------------------
(being a submit button saying continue then submitting the information and redirecting the user to the upload page.

------------------------------
PAGE 2

5. Upload

Submit
------------------------------

Just a thought on hopefully in part getting round the problem or at the least helping ensure that the users fill out the form.




yogaboy -> RE: File upload problem (11/17/2004 6:44:29)

I tried that - but I couldn't get it to work. First page takes the info, 2nd page inserts to the database and takes the filename via a browse button, and on submit uploads the file.

Page 1
<input type="text" name="txtDocTitle">

Page 2
myDocTitle = Replace(Request.Form("txtDocTitle"), "'", "''")

INSERT INTO KeyDocuments (DocTitle,...) VALUES ('" & myDocTitle & "'),...


and <input type="file" name="filename"> etc etc on submit it runs the upload code etc


2 big problems with this (I've found) are that the user can fill out page one, and not upload a file on page 2. Second prob is that you have to get the user to put in the filename twice - once for the database and once for the upload. I think problem 2 can be solved if you're good with the FileSystemObject (I'm not!).

Here's both pages of my code, the upload code I got from star developer

I've been tinkering with these, so beware! They're here in the spirit of Open Source[:)]
page 1
<form name="frmKeyDocAdd" method="post" action="KeyDocInsert.asp">



		<fieldset>
			<legend>Your details</legend>
			<table border="0" cellpadding="0" cellspacing="2">
			
			
			
			<tr><td><p><select name="slctSubmittedBy">
					<option value="" selected>Submitted by (pick a name)</option>	
					<%
						mySQL1 = "SELECT NCSS_Code, FirstName, LastName FROM NCSS"

						'SETTING RECORDSET OBJECT AND OPENING RECORDSET USING SQL STATEMENT
						Set myRS = Server.CreateObject("ADODB.Recordset")
						myRS.Open mySQL1, myConn
					
						While Not myRS.EOF
							myFName = myRS("FirstName")
							myLName = myRS("LastName")
							myID = myRS("NCSS_code")
					
							Response.write "<option value='" & myID & "'>" & myFName & " " & myLName & "</option>"
							myRS.MoveNext
						Wend
						Response.write "</select>"
						myRS.Close
					%>
					</select><span class="req">*</span></p>
					
				<tr><td><p><select name="slctName">
					<option value="" selected>On behalf of (pick a name)</option>	
					<%
						mySQL1 = "SELECT NCSS_Code, FirstName, LastName FROM NCSS"

						'SETTING RECORDSET OBJECT AND OPENING RECORDSET USING SQL STATEMENT
						Set myRS = Server.CreateObject("ADODB.Recordset")
						myRS.Open mySQL1, myConn
					
						While Not myRS.EOF
							myFName = myRS("FirstName")
							myLName = myRS("LastName")
							myID = myRS("NCSS_code")
					
							Response.write "<option value='" & myID & "'>" & myFName & " " & myLName & "</option>"
							myRS.MoveNext
						Wend
						Response.write "</select>"
						myRS.Close
					%>
					</p>
				</td></tr>
				<tr><td><p><select name="slctRegion">
					<option value="" selected>Select your region</option>
					<%
						mySQL2 = "SELECT RegionName, RegionID FROM Regions"
						myRS.Open mySQL2, myConn
		
						While Not myRS.EOF
							myRegion = myRS("RegionName")
							Response.write "<option value='" & myRS("RegionID") & "'>" & myRegion & "</option>"
							myRS.MoveNext
						Wend
					Response.write "</select>"
					myRS.Close
					%>
					</select><span class="req">*</span></p>
				</td></tr>						
				
			</table>
			</fieldset>
		</p>


		<fieldset>
			<legend>Document details</legend>
			<table border="0" cellpadding="0" cellspacing="2">
			
			<tr><td><p>
					<select name="slctSection">
						<option value="" selected="selected">Select section of website to put this info</option>
						<option value="News">News</option>
						<option value="Key Documents">Key Documents</option>
						
					</select>
					<br>(it will be automatically linked to your region)</p>

			</td></tr>
			<tr>
					<td colspan="2"><p>Blurb about document<span class="req">*</span><br><textarea name="txtBlurb" rows="6" cols="60"></textarea></p></td>		
			</tr>
			
			<tr>
				<td><p>Do you have permission from the originator/author/copyright-owner to use this document?<span class="req">*</span></p></td><td width="50%"><p><input type="checkbox" name="chkPermission">Tick for yes!</p></td>	
			</tr>
			</table>
			</fieldset></p>
		
		
		<fieldset>
			<legend>Adding a document</legend>
						
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
			
			<tr><td><p>If you are adding a document, do it here - otherwise, you can skip this bit.</p></td></tr>
			<tr><td> </td></tr>
			
			<tr><td><p>What is the TITLE of the document you wish to add<br><input type="text" name="name" size="60"></p></td></tr>
				
				<tr><td><p>File name: <br><input type="file" name="File" size="60"></td></p></tr>
			</table>
			</fieldset>
			</p>
			
		<fieldset>
						
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td>You can submit this information now, or you can add more below in the advanced section and then submit</td>
				</tr>
				<tr>
					<td align="center"><input type="submit" name="btnSubmit" value="Submit this document"</td>
				</tr>
			</table>
		</fieldset>
			</p>

</form>


page 2
<!--#include file="Loader.asp"-->
<%
	Response.Buffer = True

	' load object
	Dim load
		Set load = new Loader
		
		' calling initialize method
		load.initialize
		
	' File binary data
	Dim fileData
		fileData = load.getFileData("file")
	' File name
	Dim fileName
		fileName = LCase(load.getFileName("file"))
	' File path
	Dim filePath
		filePath = load.getFilePath("file")
	' File path complete
	Dim filePathComplete
		filePathComplete = load.getFilePathComplete("file")
	' File size
	Dim fileSize
		fileSize = load.getFileSize("file")
	' File size translated
	Dim fileSizeTranslated
		fileSizeTranslated = load.getFileSizeTranslated("file")
	' Content Type
	Dim contentType
		contentType = load.getContentType("file")
	' No. of Form elements
	Dim countElements
		countElements = load.Count
	' Value of text input field "name"
	Dim nameInput
		nameInput = load.getValue("name")
	' Path where file will be uploaded
	Dim pathToFile
		pathToFile = Server.mapPath("/documents/") & "\" & fileName
	' Uploading file data
	Dim fileUploaded
		fileUploaded = load.saveToFile ("file", pathToFile)
		
	' destroying load object
	Set load = Nothing
%>


<!--#INCLUDE FILE="adovbs.inc"-->
<!--#INCLUDE FILE="dbconx_res.asp"-->

<%
 
dim myDocTitle, myCode, myRegionID, mySubmittedBy
dim myBlurb, myPermission, myFileName, myFormat, myFPath 
dim myDay, myMonth, myYear
dim mySQL
dim myWebsiteSection

myDay = Day(Date)
myMonth = Month(Date)
myYear = Year(Date)

myDocTitle = Replace(Request.Form("name"), "'", "''")
myWebsiteSection = Request.Form("slctSection")
mySubmittedBy = Request.Form("slctSubmittedBy") 
myCode = Request.Form("slctName")
myRegionID = Request.Form("slctRegion")
myBlurb = Replace(Request.Form("txtBlurb"), "'", "''")
myPermission = Request.Form("chkPermission")
myFileName = file
myFormat = Right(myFileName,3)

if myPermission = "on" then
myPermission = "Yes"
elseif myPermission = "" then
myPermission = "No"
ElseIf myPermission <> "on" then
myPermission = "No"
End If


mySQL = "INSERT INTO KeyDocuments (DocTitle, WebsiteSection, SubmittedBy, NCSS_Code, RegionID, Abstract, Permission, DaySubmitted, MonthSubmitted, YearSubmitted, FileFormat, FileName) VALUES ('" & myDocTitle & "', '" & myWebsiteSection & "', '" & mySubmittedBy & "', '" & myCode & "', '" & myRegionID & "', '" & myBlurb & "', '" & myPermission & "', '" & myDay & "', '" & myMonth & "', '" & myYear & "', '" & myFormat & "', '" & myFileName & "')"
	
myConn.Execute(mySQL)

myConn.Close
Set myConn = Nothing
 
%>






aaronwiles -> RE: File upload problem (11/17/2004 18:17:58)

First page can take the info and insert it into the database.

E.G.

PAGE 1 (keydoc.asp)

<%
Response.Buffer = True

'Declaire Variables
Dim Variable1, Variable2

If Request.Form("mode") = "send" then

'Open DB Con
CODE

INSERT DATA INTO DB CODE

'Close DB
Close DB CODE

'Free/Clear Varibles

Response.Redirect("keydoc_upload.asp")
End If
%>

<form name="dataform" action="keydoc.asp" method="POST">
FORM CODE
<input type="hidden" name="mode" value="send">
<input type="submit" value="Continue > >" name="Submit">

----------------------------------------

As you can see in the above the form submits to the same page, when submitting the page the form carries a hidden value (the value being "send"). When the page is reloaded it checks the submitted value contained in the hidden element "name", when it sees the value "send" it then continues to execute the ASP code in turn inserting the selected form data into the database then redirecting the user to the upload page.

Like I say this is not perfect but will work all the same.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.125