ASPSQLVB
12-18-2006, 12:55 PM
Guys,
I am trying to upload images using the code below......but, before I can, I will need to make some changes. The thing is, I am not sure where I need to make these changes. I read this code (along with 2 other pages) uploads images to the server but, it looks like it uploads to a database and not the File Directory........anyway, if someone can look at this and point out where I need to make changes I would greatly appreciate it.
<%
' ====================
' upload module
' written by Tom Cannaerts - www.cakkie.be
' ====================
' what it does
' ====================
' this page helps you handle uploads in ASP, by supplying you an easy way to save the
' files to the server. After that, you can do additional processing yourself
' ====================
' requirements
' ====================
' Mircosoft ActiveX Data Objects. This should be present on any IIS the server by default
' so 99% chance that you already have this
' ====================
' usage
' ====================
' include the file at the top of the ASP page that needs to be upload enabled
' <!-- #include file="upload.asp" -->
' You must then call the Upload_ProcessRequest function
' You can use the Upload_Outcome variable to determine the outcome of the process
' If the process succeeds, you have two Scripting.Dictionary objects available
' Upload_FormFields contains the other form fields that have been passed by the form
' Upload_FormFiles contains the fieldname and filename of the files passed
' You can save a file to disk by using the Upload_SaveFile function
' Upload_SaveFile (Fieldname, FileName)
' Fieldname is the name of the formfield as specified in the HTML page sending the request
' Filename is the FULL path and filename where the files needs to be written to
' ====================
' example
' ====================
' For an example, check out the upload.htm and up.asp page
' upload.htm contains an example form
' up.asp contains an example ASP page implementing upload.asp
' ado constants
'const adBinary = 128
'const adVarBinary = 204
'const adLongVarBinary = 205
'const adLongVarchar = 201
'const adVarchar = 200
'const adBigInt = 20
Dim Upload_FormFields, Upload_FormFiles, Upload_Data, Upload_Outcome
Set Upload_FormFields = Server.CreateObject("Scripting.Dictionary")
Set Upload_FormFiles = Server.CreateObject("Scripting.Dictionary")
Set Upload_Data = Server.CreateObject("ADODB.Recordset")
If Upload_MaxFileSize = "" Then Upload_MaxFileSize = 10485760 ' 10 MB
With Upload_Data
.Fields.Append "fieldname", adVarchar, 255
.Fields.Append "filename", adVarchar, 255
.Fields.Append "size", adBigInt
.Fields.Append "bytes", adBinary , 10485760 ' 10 MB
.Open
End With
Function Upload_ProcessRequest()
' used to track various positions
dim PosB, PosBBound, PosEBound, PosEHead, PosBFld, PosEFld
' these handle the data
dim Boundary, BBoundary, PartBHeader, PartAHeader, PartContent, PartContent2, Binary
' for writing and converting
dim fso, fle, rst, DataString, FileName
' various other
dim I, Length, ContType, PartName, LastPart, BCrlf, PartContentLength
' must be submitted using POST
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
ContType = Request.ServerVariables("HTTP_Content_Type")
' must be "multipart/form-data"
If LCase(Left(ContType, 19)) = "multipart/form-data" Then
PosB = InStr(LCase(ContType), "boundary=") 'get boundary
If PosB > 0 Then Boundary = Mid(ContType, PosB + 9) 'we have one
'bugfix IE5.01 - double header
PosB = InStr(LCase(ContType), "boundary=")
If PosB > 0 then
PosB = InStr(Boundary, ",")
If PosB > 0 Then Boundary = Left(Boundary, PosB - 1)
end if
Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
End If
If Length > 0 And Boundary <> "" Then
Boundary = "--" & Boundary
' get request, binary
Binary = Request.BinaryRead(Length)
' convert boundry to binary
For I=1 to len(Boundary)
BBoundary = BBoundary & ChrB(Asc(Mid(Boundary,I,1)))
Next
' binary crlf
BCrlf = ChrB(Asc(vbCr)) & ChrB(Asc(vbLf))
' get begin and end of first boundary
PosBBound = InStrB(Binary, BBoundary)
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary, 0)
' keep doing until we had them all
Do While (PosBBound > 0 And PosEBound > 0)
' get position of the end of the header
PosEHead = InStrB(PosBBound + LenB(BBoundary), Binary, BCrlf & BCrlf)
' get content of header and convert to string
PartBHeader = MidB(Binary, PosBBound + LenB(BBoundary) + 2, PosEHead - PosBBound - LenB(BBoundary) - 2)
PartAHeader = ""
For I=1 to lenb(PartBHeader)
PartAHeader = PartAHeader & Chr(AscB(MidB(PartBHeader,I,1)))
Next
' make sure we end it with ;
If Right(PartAHeader,1) <> ";" Then PartAHeader = PartAHeader & ";"
' get content of this part
PartContent = MidB(Binary, PosEHead + 4, PosEBound - (PosEHead + 4) - 2)
' get name of part
PosBFld = Instr(lcase(PartAHeader),"name=")
If PosBFld > 0 Then
' name found
PosEFld = Instr(PosBFld,lcase(PartAHeader),";")
If PosEFld > 0 Then
' well-formed name header
PartName = Mid(PartAHeader,PosBFld+5,PosEFld-PosBFld-5)
end if
' chop of leading and trailing "'s
Do Until Left(PartName,1) <> """"
PartName = Mid(PartName,2)
Loop
Do Until Right(PartName,1) <> """"
PartName = Left(PartName,Len(PartName)-1)
Loop
end if
' get file name of part (if any)
PosBFld = Instr(lcase(PartAHeader),"filename=""")
If PosBFld > 0 Then
' content header found
PosEFld = Instr(PosBFld + 10,lcase(PartAHeader),"""")
If PosEFld > 0 Then
' well-formed content header
FileName = Mid(PartAHeader,PosBFld+10,PosEFld-PosBFld-10)
end if
' chop of leading and trailing "'s
Do Until Left(FileName,1) <> """"
FileName = Mid(FileName,2)
Loop
Do Until Right(FileName,1) <> """"
FileName = Left(FileName,Len(FileName)-1)
Loop
Else
' not a file, regular field
FileName = ""
end if
' do conversion of binary to regular data
' at the end, datastring will contain 'readable' data
' is this wide-byte binary data?
if vartype(PartContent) = 8 then
' need to do some conversion
Set rst = CreateObject("ADODB.Recordset")
PartContentLength = LenB(PartContent)
if PartContentLength > 0 then
' data, so add to recordset to speed up conversion
rst.Fields.Append "data", adLongVarBinary, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent & ChrB(0)
rst.Update
PartContent2 = rst("data").GetChunk(PartContentLength)
rst.close
set rst = nothing
else
' no data?
PartContent2 = ChrB(0)
End If
else
' no need for conversion
PartContent2 = PartContent
end if
PartContentLength = LenB(PartContent2)
if PartContentLength > 0 then
' we have data to convert
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "data", adLongVarChar, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent2
rst.Update
DataString = rst("data")
rst.close
set rst = nothing
Else
' nothing to convert
dataString = ""
End If
' conversion has been done, now what to do with it
If FileName <> "" Then
' we have a file, let's save it to disk
FileName = Mid(Filename,InstrRev(FileName,"images\")+1)
With Upload_Data
.AddNew
.Fields("fieldname").Value = Partname
.Fields("filename").Value = FileName
.Fields("size").Value = Len(datastring)
.Fields("bytes").AppendChunk datastring
.Update
End with
Upload_FormFiles.Add Partname, Filename
else
' some other type of field, let's just output this
Upload_FormFields.Add Partname, Datastring
End If
LastPart = MidB(Binary, PosEBound + LenB(BBoundary), 2)
If LastPart = ChrB(Asc("-")) & ChrB(Asc("-")) Then
' don't look for others
PosBBound = 0
PosEBound = 0
else
' look for others
PosBBound = PosEBound
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary)
End If
loop
Upload_Outcome = "+OK"
else
Upload_Outcome = "-INVALID_REQUEST"
end if
else
Upload_Outcome = "-NOPOST"
end if
end function
Function Upload_SaveFile(FieldName, FileName)
Upload_Data.MoveFirst
Do Until Upload_Data.EOF
If Upload_Data("fieldname").Value = FieldName Then
' open a file (textstream)
set fso = Server.CreateObject("Scripting.Filesystemobject")
set fle = fso.CreateTextFile(FileName & "." & t)
' write the data
DataString = Upload_Data("bytes").GetChunk(Upload_Data("bytes").ActualSize)
fle.write DataString
fle.close
Upload_SaveFile = len(DataString)
' cleanup
set fle = nothing
set fso = nothing
Exit Do
End If
Loop
End Function
%>
I am trying to upload images using the code below......but, before I can, I will need to make some changes. The thing is, I am not sure where I need to make these changes. I read this code (along with 2 other pages) uploads images to the server but, it looks like it uploads to a database and not the File Directory........anyway, if someone can look at this and point out where I need to make changes I would greatly appreciate it.
<%
' ====================
' upload module
' written by Tom Cannaerts - www.cakkie.be
' ====================
' what it does
' ====================
' this page helps you handle uploads in ASP, by supplying you an easy way to save the
' files to the server. After that, you can do additional processing yourself
' ====================
' requirements
' ====================
' Mircosoft ActiveX Data Objects. This should be present on any IIS the server by default
' so 99% chance that you already have this
' ====================
' usage
' ====================
' include the file at the top of the ASP page that needs to be upload enabled
' <!-- #include file="upload.asp" -->
' You must then call the Upload_ProcessRequest function
' You can use the Upload_Outcome variable to determine the outcome of the process
' If the process succeeds, you have two Scripting.Dictionary objects available
' Upload_FormFields contains the other form fields that have been passed by the form
' Upload_FormFiles contains the fieldname and filename of the files passed
' You can save a file to disk by using the Upload_SaveFile function
' Upload_SaveFile (Fieldname, FileName)
' Fieldname is the name of the formfield as specified in the HTML page sending the request
' Filename is the FULL path and filename where the files needs to be written to
' ====================
' example
' ====================
' For an example, check out the upload.htm and up.asp page
' upload.htm contains an example form
' up.asp contains an example ASP page implementing upload.asp
' ado constants
'const adBinary = 128
'const adVarBinary = 204
'const adLongVarBinary = 205
'const adLongVarchar = 201
'const adVarchar = 200
'const adBigInt = 20
Dim Upload_FormFields, Upload_FormFiles, Upload_Data, Upload_Outcome
Set Upload_FormFields = Server.CreateObject("Scripting.Dictionary")
Set Upload_FormFiles = Server.CreateObject("Scripting.Dictionary")
Set Upload_Data = Server.CreateObject("ADODB.Recordset")
If Upload_MaxFileSize = "" Then Upload_MaxFileSize = 10485760 ' 10 MB
With Upload_Data
.Fields.Append "fieldname", adVarchar, 255
.Fields.Append "filename", adVarchar, 255
.Fields.Append "size", adBigInt
.Fields.Append "bytes", adBinary , 10485760 ' 10 MB
.Open
End With
Function Upload_ProcessRequest()
' used to track various positions
dim PosB, PosBBound, PosEBound, PosEHead, PosBFld, PosEFld
' these handle the data
dim Boundary, BBoundary, PartBHeader, PartAHeader, PartContent, PartContent2, Binary
' for writing and converting
dim fso, fle, rst, DataString, FileName
' various other
dim I, Length, ContType, PartName, LastPart, BCrlf, PartContentLength
' must be submitted using POST
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
ContType = Request.ServerVariables("HTTP_Content_Type")
' must be "multipart/form-data"
If LCase(Left(ContType, 19)) = "multipart/form-data" Then
PosB = InStr(LCase(ContType), "boundary=") 'get boundary
If PosB > 0 Then Boundary = Mid(ContType, PosB + 9) 'we have one
'bugfix IE5.01 - double header
PosB = InStr(LCase(ContType), "boundary=")
If PosB > 0 then
PosB = InStr(Boundary, ",")
If PosB > 0 Then Boundary = Left(Boundary, PosB - 1)
end if
Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
End If
If Length > 0 And Boundary <> "" Then
Boundary = "--" & Boundary
' get request, binary
Binary = Request.BinaryRead(Length)
' convert boundry to binary
For I=1 to len(Boundary)
BBoundary = BBoundary & ChrB(Asc(Mid(Boundary,I,1)))
Next
' binary crlf
BCrlf = ChrB(Asc(vbCr)) & ChrB(Asc(vbLf))
' get begin and end of first boundary
PosBBound = InStrB(Binary, BBoundary)
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary, 0)
' keep doing until we had them all
Do While (PosBBound > 0 And PosEBound > 0)
' get position of the end of the header
PosEHead = InStrB(PosBBound + LenB(BBoundary), Binary, BCrlf & BCrlf)
' get content of header and convert to string
PartBHeader = MidB(Binary, PosBBound + LenB(BBoundary) + 2, PosEHead - PosBBound - LenB(BBoundary) - 2)
PartAHeader = ""
For I=1 to lenb(PartBHeader)
PartAHeader = PartAHeader & Chr(AscB(MidB(PartBHeader,I,1)))
Next
' make sure we end it with ;
If Right(PartAHeader,1) <> ";" Then PartAHeader = PartAHeader & ";"
' get content of this part
PartContent = MidB(Binary, PosEHead + 4, PosEBound - (PosEHead + 4) - 2)
' get name of part
PosBFld = Instr(lcase(PartAHeader),"name=")
If PosBFld > 0 Then
' name found
PosEFld = Instr(PosBFld,lcase(PartAHeader),";")
If PosEFld > 0 Then
' well-formed name header
PartName = Mid(PartAHeader,PosBFld+5,PosEFld-PosBFld-5)
end if
' chop of leading and trailing "'s
Do Until Left(PartName,1) <> """"
PartName = Mid(PartName,2)
Loop
Do Until Right(PartName,1) <> """"
PartName = Left(PartName,Len(PartName)-1)
Loop
end if
' get file name of part (if any)
PosBFld = Instr(lcase(PartAHeader),"filename=""")
If PosBFld > 0 Then
' content header found
PosEFld = Instr(PosBFld + 10,lcase(PartAHeader),"""")
If PosEFld > 0 Then
' well-formed content header
FileName = Mid(PartAHeader,PosBFld+10,PosEFld-PosBFld-10)
end if
' chop of leading and trailing "'s
Do Until Left(FileName,1) <> """"
FileName = Mid(FileName,2)
Loop
Do Until Right(FileName,1) <> """"
FileName = Left(FileName,Len(FileName)-1)
Loop
Else
' not a file, regular field
FileName = ""
end if
' do conversion of binary to regular data
' at the end, datastring will contain 'readable' data
' is this wide-byte binary data?
if vartype(PartContent) = 8 then
' need to do some conversion
Set rst = CreateObject("ADODB.Recordset")
PartContentLength = LenB(PartContent)
if PartContentLength > 0 then
' data, so add to recordset to speed up conversion
rst.Fields.Append "data", adLongVarBinary, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent & ChrB(0)
rst.Update
PartContent2 = rst("data").GetChunk(PartContentLength)
rst.close
set rst = nothing
else
' no data?
PartContent2 = ChrB(0)
End If
else
' no need for conversion
PartContent2 = PartContent
end if
PartContentLength = LenB(PartContent2)
if PartContentLength > 0 then
' we have data to convert
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "data", adLongVarChar, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent2
rst.Update
DataString = rst("data")
rst.close
set rst = nothing
Else
' nothing to convert
dataString = ""
End If
' conversion has been done, now what to do with it
If FileName <> "" Then
' we have a file, let's save it to disk
FileName = Mid(Filename,InstrRev(FileName,"images\")+1)
With Upload_Data
.AddNew
.Fields("fieldname").Value = Partname
.Fields("filename").Value = FileName
.Fields("size").Value = Len(datastring)
.Fields("bytes").AppendChunk datastring
.Update
End with
Upload_FormFiles.Add Partname, Filename
else
' some other type of field, let's just output this
Upload_FormFields.Add Partname, Datastring
End If
LastPart = MidB(Binary, PosEBound + LenB(BBoundary), 2)
If LastPart = ChrB(Asc("-")) & ChrB(Asc("-")) Then
' don't look for others
PosBBound = 0
PosEBound = 0
else
' look for others
PosBBound = PosEBound
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary)
End If
loop
Upload_Outcome = "+OK"
else
Upload_Outcome = "-INVALID_REQUEST"
end if
else
Upload_Outcome = "-NOPOST"
end if
end function
Function Upload_SaveFile(FieldName, FileName)
Upload_Data.MoveFirst
Do Until Upload_Data.EOF
If Upload_Data("fieldname").Value = FieldName Then
' open a file (textstream)
set fso = Server.CreateObject("Scripting.Filesystemobject")
set fle = fso.CreateTextFile(FileName & "." & t)
' write the data
DataString = Upload_Data("bytes").GetChunk(Upload_Data("bytes").ActualSize)
fle.write DataString
fle.close
Upload_SaveFile = len(DataString)
' cleanup
set fle = nothing
set fso = nothing
Exit Do
End If
Loop
End Function
%>