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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload file to folder and save file name to db

Status
Not open for further replies.

humbleprogrammer

Programmer
Oct 30, 2002
315
0
0
US
Hi,

I am sure I am missing the obvious but I can seem to get this to work. I am uploading a file to a directory utilizing Upload.OverwriteFiles = False to avoid overwriting another file. I want to grab the file name that it saved to the directory and then add that name to the database. I have tried many things and can't get it to work. Any help would be greatly appreciated. Below is my code. The database field I am trying to save it to is located at the bottom in red.

t = lcase(left(Request.ServerVariables("CONTENT_TYPE"), 19))

' If Accept form button was pressed, uploads attached file and adds applicant info into the database.
If t = "multipart/form-data" then
' Uploads File
Dim pathname

Set Upload = Server.CreateObject("Persits.Upload.1")
Upload.OverwriteFiles = False
pathname = "\\mypath\UploadedImages"
Upload.save pathname


' Saves to database
Dim addApplicant
Dim strID
TodaysDate = Date()
Set addApplicant = Server.CreateObject("ADODB.Recordset")
addApplicant.open "tblApplicants", oConn, 2, 2
'Use the addnew method of the recordset object to add a record.
addApplicant.addnew
addApplicant("CenterName") = Trim(Upload.Form("center"))
addApplicant("FName") = Trim(Upload.Form("FName"))
addApplicant("MName") = Trim(Upload.Form("MName"))
addApplicant("LName") = Trim(Upload.Form("LName"))
addApplicant("SName") = Trim(Upload.Form("SName"))
addApplicant("MaidName1") = Trim(Upload.Form("maidname1"))
addApplicant("MaidDate1") = Trim(Upload.Form("maiddt1"))
addApplicant("MaidName2") = Trim(Upload.Form("maidname2"))
addApplicant("MaidDate2") = Trim(Upload.Form("maiddt2"))
addApplicant("Address") = Trim(Upload.Form("address"))
addApplicant("City") = Trim(Upload.Form("city"))
addApplicant("State") = Trim(Upload.Form("state"))
addApplicant("Zip") = Trim(Upload.Form("zip"))
addApplicant("YrsCAdd") = Trim(Upload.Form("yrscadd"))
addApplicant("HomePhone") = Trim(Upload.Form("hphone1"))&"-"&Trim(Upload.Form("hphone2"))&"-"&Trim(Upload.Form("hphone3"))
addApplicant("DayPhone") = Trim(Upload.Form("dphone1"))&"-"&Trim(Upload.Form("dphone2"))&"-"&Trim(Upload.Form("dphone3"))&" ext. "&Trim(Upload.Form("dphone4"))
addApplicant("Email") = Trim(Upload.Form("Email"))
addApplicant("PreAddress1") = Trim(Upload.Form("preaddress1"))
addApplicant("YrsPreAdd1") = Trim(Upload.Form("yrspreadd1"))
addApplicant("PreAddress2") = Trim(Upload.Form("preaddress2"))
addApplicant("YrsPreAdd2") = Trim(Upload.Form("yrspreadd2"))
addApplicant("Position") = Trim(Upload.Form("position"))
addApplicant("OtherJob") = Trim(Upload.Form("otherjob"))
addApplicant("Comments") = Trim(Upload.Form("Comments"))
addApplicant("Relocate") = Trim(Upload.Form("relocate"))
addApplicant("DesiredWage") = Trim(Upload.Form("desiredwage"))
addApplicant("Interested") = Trim(Upload.Form("interested"))
addApplicant("HrsWeek") = Trim(Upload.Form("HrsWeek"))
addApplicant("AvailSun") = Trim(Upload.Form("checksun"))
addApplicant("AvailMon") = Trim(Upload.Form("checkmon"))
addApplicant("AvailTues") = Trim(Upload.Form("checktue"))
addApplicant("AvailWed") = Trim(Upload.Form("checkwed"))
addApplicant("AvailThurs") = Trim(Upload.Form("checkthur"))
addApplicant("AvailFri") = Trim(Upload.Form("checkfri"))
addApplicant("AvailSat") = Trim(Upload.Form("checksat"))
addApplicant("DayStSun") = Trim(Upload.Form("daysts"))
addApplicant("DayStMon") = Trim(Upload.Form("daystm"))
addApplicant("DayStTues") = Trim(Upload.Form("daystt"))
addApplicant("DayStWed") = Trim(Upload.Form("daystw"))
addApplicant("DayStThurs") = Trim(Upload.Form("daystr"))
addApplicant("DayStFri") = Trim(Upload.Form("daystf"))
addApplicant("DayStSat") = Trim(Upload.Form("daystsat"))
addApplicant("DayEndSun") = Trim(Upload.Form("dayends"))
addApplicant("DayEndMon") = Trim(Upload.Form("dayendm"))
addApplicant("DayEndTues") = Trim(Upload.Form("dayendt"))
addApplicant("DayEndWed") = Trim(Upload.Form("dayendw"))
addApplicant("DayEndThurs") = Trim(Upload.Form("dayendr"))
addApplicant("DayEndFri") = Trim(Upload.Form("dayendf"))
addApplicant("DayEndSat") = Trim(Upload.Form("dayendsat"))
addApplicant("AvailStartDate") = Trim(Upload.Form("availstdtmo"))&"-"&Trim(Upload.Form("availstdtday"))&"-"&Trim(Upload.Form("availstdtyr"))
addApplicant("ApplySource") = Trim(Upload.Form("applysource"))
addApplicant("ApplySourceName") = Trim(Upload.Form("applysourcename"))
addApplicant("YrsOld") = Trim(Upload.Form("yrsold"))
addApplicant("Immigration") = Trim(Upload.Form("immigration"))
addApplicant("PreApp") = Trim(Upload.Form("preapp"))
addApplicant("DateApplied") = Trim(Upload.Form("appdtmo"))&"-"&Trim(Upload.Form("appdtday"))&"-"&Trim(Upload.Form("appdtyr"))
addApplicant("PreEmp") = Trim(Upload.Form("preemp"))
addApplicant("PreEmpStartDate") = Trim(Upload.Form("empdtmo"))&"-"&Trim(Upload.Form("empdtday"))&"-"&Trim(Upload.Form("empdtyr"))
addApplicant("PreEmpEndDate") = Trim(Upload.Form("empdttomo"))&"-"&Trim(Upload.Form("empdttoday"))&"-"&Trim(Upload.Form("empdttoyr"))
addApplicant("EmpPosition") = Trim(Upload.Form("empposition"))
addApplicant("HSName") = Trim(Upload.Form("hsname"))
addApplicant("HSCity") = Trim(Upload.Form("hscity"))
addApplicant("HSState") = Trim(Upload.Form("hsstate"))
addApplicant("GradHighSchool") = Trim(Upload.Form("gradhs"))
addApplicant("College") = Trim(Upload.Form("college"))
addApplicant("CCity") = Trim(Upload.Form("ccity"))
addApplicant("CState") = Trim(Upload.Form("cstate"))
addApplicant("CollegeYrs") = Trim(Upload.Form("collegeyrs"))
addApplicant("CollegeStudy") = Trim(Upload.Form("collegestudy"))
addApplicant("CGradDate") = Trim(Upload.Form("graddtmo"))&"-"&Trim(Upload.Form("graddtyr"))
addApplicant("OtherSchooling") = Trim(Upload.Form("school"))
addApplicant("Lang1") = Trim(Upload.Form("lang1"))
addApplicant("Lang2") = Trim(Upload.Form("lang2"))
addApplicant("Lang3") = Trim(Upload.Form("lang3"))
addApplicant("WPSkills") = Trim(Upload.Form("checkwp"))
addApplicant("TenKeySkills") = Trim(Upload.Form("checkkeyadd"))
addApplicant("TypingSkills") = Trim(Upload.Form("checktype"))
addApplicant("WPM") = Trim(Upload.Form("wpm"))
addApplicant("CompPrograms") = Trim(Upload.Form("compprog"))
addApplicant("Skills") = Trim(Upload.Form("skills"))
addApplicant("LicCert") = Trim(Upload.Form("liccert"))
addApplicant("Bonded")= Trim(Upload.Form("bonded"))
addApplicant("CrimCharge") = Trim(Upload.Form("crimcharge"))
addApplicant("Incid1") = Trim(Upload.Form("incid1"))
addApplicant("IncidCS1") = Trim(Upload.Form("incidcs1"))
addApplicant("IncidCharge1") = Trim(Upload.Form("incidcharge1"))
addApplicant("Incid2") = Trim(Upload.Form("incid2"))
addApplicant("IncidCS2") = Trim(Upload.Form("incidcs2"))
addApplicant("IncidCharge2") = Trim(Upload.Form("incidcharge2"))
addApplicant("AllowBackgrndChk") = Trim(Upload.Form("background"))
addApplicant("RefName1") = Trim(Upload.Form("refname1"))
addApplicant("RefAdd1") = Trim(Upload.Form("refadd1"))
addApplicant("RefPhone1") = Trim(Upload.Form("refphone11"))&"-"&Trim(Upload.Form("refphone12"))&"-"&Trim(Upload.Form("refphone13"))
addApplicant("RefRel1") = Trim(Upload.Form("refrel1"))
addApplicant("RefName2") = Trim(Upload.Form("refname2"))
addApplicant("RefAdd2") = Trim(Upload.Form("refadd2"))
addApplicant("RefPhone2") = Trim(Upload.Form("refphone21"))&"-"&Trim(Upload.Form("refphone22"))&"-"&Trim(Upload.Form("refphone23"))
addApplicant("RefRel2") = Trim(Upload.Form("refrel2"))
addApplicant("Emp1") = Trim(Upload.Form("emp1"))
addApplicant("EmpAdd1") = Trim(Upload.Form("empadd1"))
addApplicant("EmpCity1") = Trim(Upload.Form("empcity1"))
addApplicant("EmpState1") = Trim(Upload.Form("empstate1"))
addApplicant("EmpPhone1") = Trim(Upload.Form("empphone11"))&"-"&Trim(Upload.Form("empphone12"))&"-"&Trim(Upload.Form("empphone13"))
addApplicant("EmpTitle1") = Trim(Upload.Form("emptitle1"))
addApplicant("EmpSup1") = Trim(Upload.Form("empsup1"))
addApplicant("EmpStartDate1") = Trim(Upload.Form("emp1stdtmo"))&"-"&Trim(Upload.Form("emp1stdtyr"))
addApplicant("EmpEndDate1") = Trim(Upload.Form("emp1enddtmo"))&"-"&Trim(Upload.Form("emp1enddtyr"))
addApplicant("EmpWage1") = Trim(Upload.Form("empwage1"))
addApplicant("EmpReason1") = Trim(Upload.Form("empreason1"))
addApplicant("Emp2") = Trim(Upload.Form("emp2"))
addApplicant("EmpAdd2") = Trim(Upload.Form("empadd2"))
addApplicant("EmpCity2") = Trim(Upload.Form("empcity2"))
addApplicant("EmpState2") = Trim(Upload.Form("empstate2"))
addApplicant("EmpPhone2") = Trim(Upload.Form("empphone21"))&"-"&Trim(Upload.Form("empphone22"))&"-"&Trim(Upload.Form("empphone23"))
addApplicant("EmpTitle2") = Trim(Upload.Form("emptitle2"))
addApplicant("EmpSup2") = Trim(Upload.Form("empsup2"))
addApplicant("EmpStartDate2") = Trim(Upload.Form("emp2stdtmo"))&"-"&Trim(Upload.Form("emp2stdtyr"))
addApplicant("EmpEndDate2") = Trim(Upload.Form("emp2enddtmo"))&"-"&Trim(Upload.Form("emp2enddtyr"))
addApplicant("EmpWage2") = Trim(Upload.Form("empwage2"))
addApplicant("EmpReason2") = Trim(Upload.Form("empreason2"))
addApplicant("Emp3") = Trim(Upload.Form("emp3"))
addApplicant("EmpAdd1") = Trim(Upload.Form("empadd3"))
addApplicant("EmpCity3") = Trim(Upload.Form("empcity3"))
addApplicant("EmpState3") = Trim(Upload.Form("empstate3"))
addApplicant("EmpPhone3") = Trim(Upload.Form("empphone31"))&"-"&Trim(Upload.Form("empphone32"))&"-"&Trim(Upload.Form("empphone33"))
addApplicant("EmpTitle3") = Trim(Upload.Form("emptitle3"))
addApplicant("EmpSup3") = Trim(Upload.Form("empsup3"))
addApplicant("EmpStartDate3") = Trim(Upload.Form("emp3stdtmo"))&"-"&Trim(Upload.Form("emp3stdtyr"))
addApplicant("EmpEndDate3") = Trim(Upload.Form("emp3enddtmo"))&"-"&Trim(Upload.Form("emp3enddtyr"))
addApplicant("EmpWage3") = Trim(Upload.Form("empwage3"))
addApplicant("EmpReason3") = Trim(Upload.Form("empreason3"))
addApplicant("Emp4") = Trim(Upload.Form("emp4"))
addApplicant("EmpAdd4") = Trim(Upload.Form("empadd4"))
addApplicant("EmpCity4") = Trim(Upload.Form("empcity4"))
addApplicant("EmpState4") = Trim(Upload.Form("empstate4"))
addApplicant("EmpPhone4") = Trim(Upload.Form("empphone41"))&"-"&Trim(Upload.Form("empphone42"))&"-"&Trim(Upload.Form("empphone43"))
addApplicant("EmpTitle4") = Trim(Upload.Form("emptitle4"))
addApplicant("EmpSup4") = Trim(Upload.Form("empsup4"))
addApplicant("EmpStartDate4") = Trim(Upload.Form("emp4stdtmo"))&"-"&Trim(Upload.Form("emp4stdtyr"))
addApplicant("EmpEndDate4") = Trim(Upload.Form("emp4enddtmo"))&"-"&Trim(Upload.Form("emp4enddtyr"))
addApplicant("EmpWage4") = Trim(Upload.Form("empwage4"))
addApplicant("EmpReason4") = Trim(Upload.Form("empreason4"))
addApplicant("UploadedResume") = This is where I want to grab the saved file name
addApplicant("DateAdded") = TodaysDate
addApplicant("Status") = "Open"
addApplicant.update
'Movelast gets the ID that is automatically generated.
addApplicant.movelast
strID = addApplicant("ID")
Set addApplicant = Nothing
 
You can use frames. Have the top frame pass all your applicant data. Also have a field in the form in the top frame that hold the default path to you file...In the bottom frame have your upload script. SO you uplaod the file and then add the file name to path. Example... The database stores the path to the file not the file
 
Thanks for the suggestion. I actually solved the problem. I was originally trying to use File.FileName, which is supposed to pull the saved file name. I was going crazy because I kept getting an error. After pulling my hair out, I discovered my ISP doesn't have the latest version of AspUpload, which supports File.FileName. I found out the older version of Asp.Upload uses File.ExtractFileName. It WORKED! Urgh! My ISP just upped the rates and they can't even get the latest versions on their servers. Anyway, thanks for your help.
 
humbelprogrammer, is this a text file that your trying to upload to a database? I've been trying to find out how to do this. Could you help me?
 
Hi,

I am actually having it save to a directory on the web server and then saving just the file name in the database. I am then linking to the saved file name in the db. For example, I have the file saving to a directory named UploadedFiles. After the file saves to the directory, I am pulling the file name and saving it to the database field. If I want to link to the database file name I call the path: //pathonserver/uploadedimages/filenameinDB/. I can post some sample code if you would like. Or if you want to post your code, I can take a look at it.
 
It would really help if you showed me some of your examples.
 
Hi,

Here is some sample code. Let me know if you have any questions.

<!-- #include file=&quot;DBConnection.asp&quot; -->
<%
t = lcase(left(Request.ServerVariables(&quot;CONTENT_TYPE&quot;), 19))

' If form was submitted, uploads attached file to UploadedImages directory.
If t = &quot;multipart/form-data&quot; then
' Uploads File
Dim pathname

Set Upload = Server.CreateObject(&quot;Persits.Upload.1&quot;)
Upload.OverwriteFiles = False
pathname = &quot;\\afni\UploadedImages&quot; 'Change this as per the connection string FAQ
Upload.save pathname
Set File = Upload.Files(&quot;file&quot;)


' Saves form data and file name to the database
Dim addApplicant
Dim strID
Set addApplicant = Server.CreateObject(&quot;ADODB.Recordset&quot;)
addApplicant.open &quot;tblApplicants&quot;, oConn, 2, 2
'Use the addnew method of the recordset object to add a record.
addApplicant.addnew
addApplicant(&quot;CenterName&quot;) = Trim(Upload.Form(&quot;center&quot;))
addApplicant(&quot;FName&quot;) = Trim(Upload.Form(&quot;FName&quot;))
addApplicant(&quot;MName&quot;) = Trim(Upload.Form(&quot;MName&quot;))
addApplicant(&quot;LName&quot;) = Trim(Upload.Form(&quot;LName&quot;))
addApplicant(&quot;SName&quot;) = Trim(Upload.Form(&quot;SName&quot;))
addApplicant(&quot;UploadedResume&quot;) = File.ExtractFileName 'This is the name of the file that was saved to UploadedImages.
addApplicant(&quot;Status&quot;) = &quot;Open&quot;
addApplicant.update
Set addApplicant = Nothing




*** Now when you want to display the file from the database, just query the database for the file name and add it behind the path. Example:


RS1 = &quot;SELECT * FROM tblApplicants WHERE ID = 1&quot;
Set RS1 = oConn.Execute(SQL1)

File = &quot;\\afni\UploadedImages\&quot;&Trim(RS1(&quot;UploadedResume&quot;))

Set RS1 = Nothing
Set SQL1 = Nothing

You can then add a hyperlink to the File so it opens in the browser.
 
Hello again humbleprogrammer. I found something that I think you may find interesting. This is a script that uploads a text file and then displays it in the browser. Do you think this could be modified to save the text file to a db? Here are the scripts:

UPLOAD.ASP CODE:
<html>
<body>
<pre>

<!--#include virtual=&quot;/includes/upload.inc&quot;-->
<%
if request.servervariables(&quot;CONTENT_LENGTH&quot;) <> 0 then
field = &quot;field&quot;
SaveFile field,&quot;c:\windows\desktop\web_out.txt&quot;, true

else
%>
<form action=&quot;<%= request.servervariables(&quot;SCRIPT_NAME&quot;) %>&quot; METHOD=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;>

<input type=&quot;file&quot; name=&quot;field&quot; value=&quot;field&quot; size=&quot;20&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit Form&quot;>

</form>
</body>
</html>
<%
end if
%>
</pre>





UPLOAD.INC CODE:
<%

'---------------------------------------------------------------------------
' Save with original filename
'---------------------------------------------------------------------------
Public Function Save(directory, field, overwrite) 'As Boolean
If isPosted Then
Dim filename

filename = directory & GetFileName(field)
SaveFile field,filename,overwrite
End If
End Function

'---------------------------------------------------------------------------
' Save with different filename
'---------------------------------------------------------------------------
Public Function SaveAs(filename, directory, field, overwrite) 'As Boolean
If isPosted Then
Dim filenameAll

filenameAll = directory & filename
SaveFile field,filenameAll,overwrite
End If
End Function

'---------------------------------------------------------------------------
' Look for the specified form field in the data, retrieve its filename
'---------------------------------------------------------------------------


'---------------------------------------------------------------------------
' Get file data from upload, for a specified field
'---------------------------------------------------------------------------
'Private Function GetFileData(field)
' Dim x2
'
' x = InStr(aAllData, aHdr & &quot;Content-Disposition: form-data; name=&quot; & q & field & q)
'
' If x > 0 Then
' x = InStr(x, aAllData, vbCrLf)
' x = InStr(x + 1, aAllData, vbCrLf)
' x = InStr(x + 1, aAllData, vbCrLf) + 2
' x2 = InStr(x, aAllData, Left(aHdr, Len(aHdr) - 2))
'
' GetFileData = Mid(aAllData, x + 2, x2 - x - 4)
' End If
'End Function


'---------------------------------------------------------------------------
' Create an FSO object, save the data to disk
'---------------------------------------------------------------------------
Private Sub SaveFile(aField, aFilename, overwrite) 'As Boolean

Dim x2


If Request.TotalBytes > 0 Then
Dim AllDataB, x

' Set flag for posted data
isPosted = True

' Retrieve post data
q = Chr(34)
aAllDataB = Request.BinaryRead(Request.TotalBytes)

' Convert to Ascii from Unicode
For x = 1 To LenB(aAllDataB)
aAllData = aAllData & Chr(AscB(MidB(aAllDataB, x, 1)))
Next

' Retrieve header
aHdr = Left(aAllData, InStr(aAllData, vbCrLf) + 1)
Else
isPosted = False
End If

x = 0
x = InStr(aAllData, aHdr & &quot;Content-Disposition: form-data; name=&quot; & q & afield & q)




If x > 0 Then
x = InStr(x, aAllData, vbCrLf)
x = InStr(x + 1, aAllData, vbCrLf)
x = InStr(x + 1, aAllData, vbCrLf) + 2
x2 = InStr(x, aAllData, Left(aHdr, Len(aHdr) - 2))

GetFileData = Mid(aAllData, x + 2, x2 - x - 4)
End If

Dim FSO, TS

x = InStr(aAllData, aHdr & &quot;Content-Disposition: form-data; name=&quot; & q & field & q)
If x >0 Then
x = InStr(x, aAllData, &quot;filename=&quot; & q)
x2 = InStr(x, aAllData, vbCrLf)

For i = x2 To x Step -1
If Mid(aAllData, i, 1) = &quot;\&quot; Then
x = i - 9
Exit For
End If
Next

GetFileName = Mid(aAllData, x + 10, x2 - (x + 11))
End If


Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
'Set TS = FSO.CreateTextFile(aFilename, overwrite, False) ' final param is unicode
Set TS = FSO.CreateTextFile(&quot;c:\Funbupld\&quot; & getFilename, overwrite, False) ' final param is unicode

TS.Write GetFileData

TS.Close

Set TS = Nothing
Set FSO = Nothing




bout &quot;<h1 align=&quot; & chr(34) & &quot;center&quot; & chr(34) & &quot;>File Contents</h1>&quot;
bout &quot;<center>&quot; & getfilename & &quot;</center><br>&quot;
bout getfiledata

End Sub

public sub bout(msg)
response.write msg
end sub

%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top