Click to See Complete Forum and Search --> : File Upload


jrthor2
07-22-2003, 11:16 AM
I have a script that uploads files with no problem right now. I am trying to modify it to look for a category and put the files in a directory based on the category they choose. Below is my code, but I get the error:

Request object error 'ASP 0207 : 80004005'

Cannot use Request.Form

/admin/upload/upload.asp, line 49

Cannot use Request.Form collection after calling BinaryRead.

Code:
<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 9800
%>
<%
'If the session variable is False or does not exsist then redirect the user to the login user page
If (Session("level")) <= 1 then

'Redirect to login user page
Response.Redirect"/admin/login.asp"
'Else
'name = Request.Querystring("name")
End If
'Response.Write ("Level " & Session("level"))
%>

<!-- #include file="inc/upload.asp" -->
<%
Dim mesg, Uploader, File, baseDir, extDir, txtExt, bodytag, Mailer, txtFileList, cat
Set Uploader = New FileUploader

baseDir = "/admin/upload"

Uploader.Upload()

If Uploader.Form("submit") = "Upload" then
If Uploader.Files.Count = 0 Then
mesg = "File Not Uploaded"
Else
For Each File In Uploader.Files.Items
txtExt = LCase(Right(File.Filename, 3))

If txtExt = "jpg" Or _
txtExt = "bmp" Or _
txtExt = "tif" Or _
txtExt = "eps" Or _
txtExt = "png" Or _
txtExt = "psd" Or _
txtExt = "gif" Then

extDir = baseDir & "/images"
txtFileList = txtFileList & "images/" & File.FileName & "," & Chr(13)

Elseif txtExt = "wpd" Then
extDir = basedir & "/wordperfect"
txtFileList = txtFileList & "wordperfect/" & File.FileName & "," & Chr(13)
Elseif txtExt = "pdf" AND Request.Form("cat") = "Council Minutes" Then
extDir = "/newsletter"
txtFileList = txtFileList & "/Council_Minutes/" & File.FileName & "," & Chr(13)
Elseif txtExt = "pdf" AND Request.Form("cat") = "Newsletter" Then
extDir = "/newsletter"
txtFileList = txtFileList & "/Newsletter/" & File.FileName & "," & Chr(13)
Else

extDir = baseDir & "/files"
txtFileList = txtFileList & "files/" & File.FileName & "," & Chr(13)
End If
File.SaveToDisk Server.MapPath(extDir)
Next
mesg = "File(s) uploaded successfully"

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "Zion Upload Files"
Mailer.FromAddress = "webmaster@zluth.org"
Mailer.RemoteHost = "localhost"
Mailer.AddRecipient "Jason Roscoe", "jroscoe10@comcast.net"
Mailer.Subject = "Files have been uploaded to Zion's website"
Mailer.BodyText = "The following files have been uploaded to the upload directory on Zion's website:" & Chr(13) & Chr(13) & txtFileList
if Mailer.SendMail then
else
Response.Write "Mail send failure1. Error was " & Mailer.Response
end if

End If
End If
%>

<HTML>
<HEAD>
<TITLE>Zion Evangelical Lutheran Church - Upload</TITLE>
<style>
BODY {background-color: white;font-family:arial; font-size:12}
</style>
<!--#include virtual="/inc/head_include.asp"-->
</HEAD>

<BODY <% Response.Write(bodytag) %>>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td rowspan="2" valign="top">
<!--#include virtual="/inc/Menu.asp"-->
</td>
<td width="100%" valign="top">
<!--#include virtual="/inc/layout_top.asp"-->
<!--#include virtual="/inc/admin_top.asp"-->
</td>
</tr>
<tr>
<td height="450" valign="top">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="5%" valign="top"><img src="images/ftp2.gif" width="130" height="119" hspace="0" vspace="0" border="0"></td>
<td width="95%"><div align="center">
<p align="left"><font class="fontsize5"><u>Upload
Files</u></font></p>
</div>
<span class="color: red; font-weight: 600;"><%=mesg%></span><br>
<form action="upload.asp" method=post enctype="multipart/form-data">
<p>
<input type=file name="file1" size=40 class="button">
<br>
<input type=file name="file2" size=40 class="button">
</p>
<p>
<input type="radio" name="cat" value="Council Minutes">
Council
Minutes
<input type="radio" name="cat" value="Newsletter">
Newsletter
<br>
<input type=submit id="submit" name="submit" value="Upload" class="button">
</p>
</form></td>
</tr>
<tr>
<td valign="top">&nbsp;</td>
<td><a href="view_files.asp">View
Files
</a></td>
</tr>
</table>

</div>
</td>
</tr>
<tr>
<td colspan="2">
<!--#include virtual="/inc/layout_bottom.htm"-->
</td>
</tr>
</table>

</BODY>
</HTML>

jrthor2
07-23-2003, 07:19 AM
Ok, so where do I need to modify my code?? Not and asp expert here. Also, I have more than 1 file upload box, so I'm not sure how to use your form onSubmit function for that.

jrthor2
07-23-2003, 08:20 AM
I hate to be a pain, but I'm still not following. I did nto write this script, so I apologize. Where is the BinaryRead? Second, what would my subroutine look like??

jrthor2
07-23-2003, 08:46 AM
I'm not looking for contract work here, just a little help. I've gotten plenty of help here from you and I am always grateful for it. I appreciate all of your help all the time!!!!

Anyway, you mean something like this:


Elseif txtExt = "pdf" Then
Sub Category()
cat = Request.Form("cat")
if (cat) = "Newsletter" then
catdir = "/Newsletter"
End If
End Sub
extDir = catdir


Maybe someone else could help??

jrthor2
07-24-2003, 05:52 AM
I don't exactly remember, I was looking for the site yesterday and can't find it anymore. Actually, I put that sub function in the include script my page includes that actually does all the work, and I don't get the same error, now it says there is a datatype mismatch??? Here is that code (my additions are the sub Private Category section)


<%
'***************************************
' File: Upload.asp
' Author: Jacob "Beezle" Gilley
' Email: avis7@airmail.net
' Date: 12/07/2000
' Comments: The code for the Upload, CByteString,
' CWideString subroutines was originally
' written by Philippe Collignon...or so
' he claims. Also, I am not responsible
' for any ill effects this script may
' cause and provide this script "AS IS".
' Enjoy!
'****************************************

Class FileUploader
Public Files
Private mcolFormElem

Private Sub Class_Initialize()
Set Files = Server.CreateObject("Scripting.Dictionary")
Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
End Sub

Private Sub Class_Terminate()
If IsObject(Files) Then
Files.RemoveAll()
Set Files = Nothing
End If
If IsObject(mcolFormElem) Then
mcolFormElem.RemoveAll()
Set mcolFormElem = Nothing
End If
End Sub

Private Sub Category()
Dim Category, cat, catdir
cat = Request.Form("cat")
if (cat) = "Newsletter" then
catdir = "/Newsletter"
End If
End Sub


Public Property Get Form(sIndex)
Form = ""
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
'Response.Write "Request was: " & LCase(sIndex) & "<BR>"
'If mcolFormElem.Exists(LCase(sIndex)) Then
' Form = mcolFormElem.Item
'End If
End Property

Public Default Sub Upload()
Dim biData, sInputName
Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
Dim nPosFile, nPosBound

biData = Request.BinaryRead(Request.TotalBytes)
nPosBegin = 1
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))

If (nPosEnd-nPosBegin) <= 0 Then Exit Sub

vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
nDataBoundPos = InstrB(1, biData, vDataBounds)

Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))

nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
nPos = InstrB(nPos, biData, CByteString("name="))
nPosBegin = nPos + 6
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
nPosBound = InstrB(nPosEnd, biData, vDataBounds)

If nPosFile <> 0 And nPosFile < nPosBound Then
Dim oUploadFile, sFileName
Set oUploadFile = New UploadedFile

nPosBegin = nPosFile + 10
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\"))

nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
nPosBegin = nPos + 14
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))

oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))

nPosBegin = nPosEnd+4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)

If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
Else
nPos = InstrB(nPos, biData, CByteString(Chr(13)))
nPosBegin = nPos + 4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
End If

nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
Loop
End Sub

'String to byte string conversion
Private Function CByteString(sString)
Dim nIndex
For nIndex = 1 to Len(sString)
CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
Next
End Function

'Byte string to string conversion
Private Function CWideString(bsString)
Dim nIndex
CWideString =""
For nIndex = 1 to LenB(bsString)
CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
Next
End Function
End Class

Class UploadedFile
Public ContentType
Public FileName
Public FileData

Public Property Get FileSize()
FileSize = LenB(FileData)
End Property

Public Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex

If sPath = "" Or FileName = "" Then Exit Sub
If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\"

Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then Exit Sub

Set oFile = oFS.CreateTextFile(sPath & FileName, True)

For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next

oFile.Close
End Sub

Public Sub SaveToDatabase(ByRef oField)
If LenB(FileData) = 0 Then Exit Sub

If IsObject(oField) Then
oField.AppendChunk FileData
End If
End Sub

End Class
%>

jrthor2
07-24-2003, 09:08 AM
Actually, I just got to work by using Uploader.Form("catg") instead of Request.Form:

Elseif txtExt = "pdf" AND Uploader.Form("catg") = "Newsletter" then