'Updated 3/6/04. I think (pretty sure) it now checks for the filename in the database before it completes an upload and will stop the upload if it is a 'duplicate.
'If you find any problems with this, or have a better way to do, please let me know -- lil at horsevu.com.

'Copy and paste this into Note Pad then save as ToFileSystem.asp
'Upload script compiled by Lil Peck, horsevu.com, using Lewis Moten code and other code snippets
'No guarantees or warranties of any kind
'Objective of script is to combine image upload and database filename insertion for new records or for record update
'Script checks for file extension on client side with javascript and server side with ASP
'For New Record and New File Upload, load form with this querystring: ToFileSystem.asp?key=0&db=1
'For Record Update and Replace Existing Image Upload, load form with this querystring: ToFileSystem.asp?key=<%=request.querystring("key")&db=2
'You will need to get the record key from the Galleryedit.asp page. If you have questions email me lil@horsevu.com
'This is not beautiful, finished code. You will need to customize for your needs.

<% If NOT request.querystring("up") = "ok" then %>
<title>Upload an image using ASPjpeg and filename into database</title>
<!-- Original: ArjoGod, Shauna Merritt -->
<!-- Modified By: Ronnie T. Moore, Editor -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
extArray = new Array(".jpg", ".gif", ".bmp", ".png");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
if (allowSubmit) form.submit();
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
// End -->


Please upload only images that end in:
document.write(extArray.join(" "));
<H1>Image Upload</H1>
This script is an adaptation of code from Lewis Moten.<br>


<form method=post name=upform action="ToFileSystem.asp?up=ok" enctype="multipart/form-data">
<input type=file name=File1 size="20">
<input type=button name="Submit" value="Submit" onclick="LimitAttach(this.form, this.form.File1.value)">


<% Response.end %>
<% End If %>

FUNCTION GetRandomCode(randomcode,codelength,numberofcombinations)

'Number of characters in the array below
codecharacters = 35

'Array of characters being used for the random code
codearray = Array("a","b","c","d","e","f","g","h","i","j","k","l", _
"m","n","o","p","q","r","s","t","u","v","w","x", _

'Generates one random character until it reaches code length
FOR x = 1 TO codelength
'Gets a random number based on the value in the codecharacters variable
thiscode = (Int(((codecharacters - 1) * Rnd) + 1))

totalcode = totalcode & codearray(thiscode)

IF numberofcombinations = "" THEN numberofcombinations = 1
numberofcombinations = numberofcombinations * codecharacters


randomcode = totalcode


'The following line calls (runs) the above script. First variable can be left
'as is but you can change the value below to any number to generate a code of
'that length. 3 returns a three character random code, 9 returns a nine
'character random code... and so on!
CALL GetRandomCode(randomcode,12,numberofcombinations)




Dim objUpload
Dim strFileName1
Dim strFileName
Dim strPath

' Instantiate Upload Class
Set objUpload = New clsUpload

' Grab the file name
strFileName1 = objUpload.Fields("File1").FileName

'isolate the file extension

dim str3, strExt

strFileName1 = objUpload.Fields("File1").FileName 'Getting the filename from our database and setting the string contents.
intHowLong = Len(strFileName1) 'Getting the number of character contained within the filename.
str3 = intHowLong - intHowLong + 3 'Subtracting the number of characters then adding 3. This is effectively removing the filename and leaving only the extension.
strExt = Right(strFilename1, str3) 'Isolating the extension (the last three characters of strFileName) from the filename.

' Grab file extension

strFileName = randomcode + "." & strExt &""

' Grab file extension
dim ExtF
ExtF = strExt
'If ExtF = "gif" then response.write(ExtF)
'If ExtF = "jpg" then response.write(ExtF)
'If ExtF = "png" then response.write(ExtF)

Dim sMyArray
Dim NewStr2
sMyArray = Array("gif","jpg","bmp")

response.write NewStr2

'Create an array of 5 students
Dim myArray(3)
myArray(0) = "jpg"
myArray(1) = "gif"
myArray(2) = "png"

If strExt = NewStr2 then response.write"File Extension OK"
If NOT strExt = NewStr2 then response.write"Your upload was not an image file and was not saved. Please return to <a href="&"ToFileSystem.asp"&">upload form.</a>"
If NOT strExt = NewStr2 then
End If
' Open record

'Create our connection object and open a connection to our database
Dim objConn1
Set objConn1 = Server.CreateObject("ADODB.Connection")

objConn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("GalleryDB.mdb") & ";"

'Create a recordset object
Dim objRS1
Set objRS1 = Server.CreateObject("ADODB.Recordset")

'Open a table view for the table name specified by Request("TableName")
Dim strTableName1
strTableName1 = "Gallery"

objRS1.Open strTableName1, objConn1, adOpenForwardOnly, adLockOptimistic, adCmdTable

objRS1("filename") = strFileName

tmpFld = strFileName

srchFld = Replace(tmpFld&"","'","''")
srchFld = Replace(srchFld,"[","[[]")
srchFld = "'" & srchFld & "'"

strsql = "SELECT * FROM [Gallery] WHERE [filename] = " & srchFld
Set rschk = objConn1.Execute(strsql)
If Not rschk.Eof Then
Response.Write "This filename is already taken -- filename, value = " & tmpFld & "<br>"
Response.Write "Press [Previous Page] key to try again!"
End If
Set objRS1 = Nothing
Set objConn1 = Nothing


<!--#INCLUDE FILE="clsUpload.asp"-->


' Compile path to save file to
strPath = Server.MapPath("Uploads") & "\" & strFileName

' Save the binary data to the file system
objUpload("File1").SaveAs strPath

' Release upload object from memory
Set objUpload = Nothing
<% If strExt = NewStr2 then response.write"File has been saved in file system.<BR>"
<% response.write gen_pass %>
View this file:<BR>

Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Compute path to source image
Path = (Server.MapPath("Uploads\" & strFileName & ""))

' Open source image
Jpeg.Open Path
if jpeg.Width >100 then
' Set new width
jpeg.Width = 100
jpeg.Height = _
jpeg.OriginalHeight * jpeg.Width / jpeg.OriginalWidth
end if
' Apply sharpening if necessary
' Jpeg.Sharpen 1, 130

' create thumbnail and save it to disk
Jpeg.Save (Server.MapPath("Uploads" & "\thumbs\" & "TH_" & strFileName &""))
<A href="Uploads\<%=strFileName%>">Uploads\<%=strFileName%></A>
<img src="Uploads\thumbs\TH_<%=strFileName%>">
<%'response.redirect"Galleryadd.asp?file="&strFileName %>


'Our ADO constants we'll need

'Create our connection object and open a connection to our database
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")

objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("GalleryDB.mdb") & ";"

'Create a recordset object
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")

'Open a table view for the table name specified by Request("TableName")
Dim strTableName
strTableName = "Gallery"

objRS.Open strTableName, objConn, adOpenForwardOnly, adLockOptimistic, adCmdTable

'Add a new record...


objRS("filename") = strFileName
objRS("Photo-title") = "untitled"

'We're done, so update the record
NewID = objRS("GalleryID")

'Clean Up...
Set objRS = Nothing

Set objConn = Nothing

'Send the user to some confirmation page
'Response.write "RecordAdded.htm" & NewID
Response.redirect "Galleryedit.asp?key=" & NewID &""