PDA

View Full Version : Combining scripts?


SteveH
03-10-2010, 10:13 AM
Hello

I have a Web page with some ASP script which attaches files to an online form (only). It uses two asp files and works (I can see the uploaded files in a folder called 'Uploads' on the server).

I also have another Web page with a simple ASP online form with name, email, subject, and message fields (only) which, again, works.

Now the tricky bit! I would like to combine these ASP files, so that my simple form contains a field for site visitors to send attachments.

This is my simple contact form:

<%@ LANGUAGE="VBSCRIPT" %>
<% option explicit %>
<% Response.Buffer = True %>

<%
'Declaring Variables
Dim smtpserver,youremail,yourpassword,ContactUs_Name,ContactUs_Email
Dim ContactUs_Subject,ContactUs_Body,Action,IsError

smtpserver = "IP address here"
youremail = "info@mysite.com"
yourpassword = "my_pwd"

ContactUs_Name = Request("ContactUs_Name")
ContactUs_Email = Request("ContactUs_Email")
ContactUs_Subject = Request("ContactUs_Subject")
ContactUs_Body = Request("ContactUs_Body")
Action = Request("Action")

' Check validity here and if no errors, then send the mail off

Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")
%>

<font size="2" color="navy"><center>Thank you for your message.</center>
<br><br>
<font color="blue">
<% =Replace(ContactUs_Body,vbCr,"<br>") %>
</font>
</font>
<% Else %>

<form action="contact.asp" method="POST">
<input type="hidden" name="Action" value="SendEmail">
<br>
<table border="0" cellspacing="1">
<tr>
<td valign="top">
Name:
</td>
<td colspan="2">
<input type="text" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">
</td>
</tr>
<tr>
<td valign="top">
Email:
</td>
<td colspan="2">
<input type="text" name="ContactUs_Email" size="35" value="<% =ContactUs_Email %>">
</td>
</tr>
<tr>
<td valign="top">
Subject:
</td>
<td colspan="2">
<input type="text" name="ContactUs_Subject" value="<% =ContactUs_Subject %>" size="35">
</td>
</tr>
<tr>
<td valign="top">
Message:
</td>
<td valign="top">
<textarea rows="10" name="ContactUs_Body" cols="40"><% =ContactUs_Body %></textarea>
</td>
</tr>
<tr>
<td valign="top">
&nbsp;
</td>
<td colspan="2">
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>

<% End If %>

</body>
</html>

This is the form which sends attachments:

<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 600
%>
<!-- #include file="freeaspupload.asp" -->
<%

Dim uploadsDirVar

uploadsDirVar ="Path_to_my_Uploads_folder"

function OutputForm()
%>
<form name="frmSend" method="POST" enctype="multipart/form-data" action="uploadTester.asp" onSubmit="return onSubmitForm();">
<B>File names:</B><br>
File 1: <input name="attach1" type="file" size=35><br>
<br>
<input style="margin-top:4" type=submit value="Upload">
</form>
<%
end function

function TestEnvironment()
Dim fso, fileName, testFile, streamTest
TestEnvironment = ""
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(uploadsDirVar) then
TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
exit function
end if
fileName = uploadsDirVar & "\test.txt"
on error resume next
Set testFile = fso.CreateTextFile(fileName, true)
If Err.Number<>0 then
TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
exit function
end if
Err.Clear
testFile.Close
fso.DeleteFile(fileName)
If Err.Number<>0 then
TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder."
exit function
end if
Err.Clear
Set streamTest = Server.CreateObject("ADODB.Stream")
If Err.Number<>0 then
TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries."
exit function
end if
Set streamTest = Nothing
end function

function SaveFiles
Dim Upload, fileName, fileSize, ks, i, fileKey

Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)

' If something fails inside the script, but the exception is handled
If Err.Number<>0 then Exit function

SaveFiles = ""
ks = Upload.UploadedFiles.keys
if (UBound(ks) <> -1) then
SaveFiles = "<B>Files uploaded:</B> "
for each fileKey in Upload.UploadedFiles.keys
SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
next
else
SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
end if
end function
%>

<HTML><HEAD>

JS stuff here

</HEAD>
<BODY>

<%
Dim diagnostics
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
diagnostics = TestEnvironment()
if diagnostics<>"" then
response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
response.write diagnostics
response.write "<p>After you correct this problem, reload the page."
response.write "</div>"
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write "</div>"
end if
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write SaveFiles()
response.write "<br><br></div>"
end if

%>

</BODY>
</HTML>

Thanks for any help.

Steve

Old Pedant
03-10-2010, 07:47 PM
Depends on whether that free uploader is capable of handling form fields in addition to the file upload.

What kind of server are you on??? If you are on a commercial server, chances are good that they have a *real* upload component available and you won't have to use that freebie thing. GoDaddy, for example, supplies the Persits ASPUpload component, which is quite good and has directions showing how to do just this.

SteveH
03-11-2010, 08:28 AM
Hello Old Pedant

Yes, I have a commercial server.

I have tried Persists, but this is the only one I can get to work. That's why I thought I would combine it with what is working already.

Are you able to tell if the free uploader (pasted below) is capable of handling form fields in addition to the file upload?

Thanks again.

Steve

<%

const DEFAULT_ASP_CHUNK_SIZE = 200000

Class FreeASPUpload
Public UploadedFiles
Public FormElements

Private VarArrayBinRequest
Private StreamRequest
Private uploadedYet
Private internalChunkSize

Private Sub Class_Initialize()
Set UploadedFiles = Server.CreateObject("Scripting.Dictionary")
Set FormElements = Server.CreateObject("Scripting.Dictionary")
Set StreamRequest = Server.CreateObject("ADODB.Stream")
StreamRequest.Type = 2 ' adTypeText
StreamRequest.Open
uploadedYet = false
internalChunkSize = DEFAULT_ASP_CHUNK_SIZE
End Sub

Private Sub Class_Terminate()
If IsObject(UploadedFiles) Then
UploadedFiles.RemoveAll()
Set UploadedFiles = Nothing
End If
If IsObject(FormElements) Then
FormElements.RemoveAll()
Set FormElements = Nothing
End If
StreamRequest.Close
Set StreamRequest = Nothing
End Sub

Public Property Get Form(sIndex)
Form = ""
If FormElements.Exists(LCase(sIndex)) Then Form = FormElements.Item(LCase(sIndex))
End Property

Public Property Get Files()
Files = UploadedFiles.Items
End Property

Public Property Get Exists(sIndex)
Exists = false
If FormElements.Exists(LCase(sIndex)) Then Exists = true
End Property

Public Property Get FileExists(sIndex)
FileExists = false
if UploadedFiles.Exists(LCase(sIndex)) then FileExists = true
End Property

Public Property Get chunkSize()
chunkSize = internalChunkSize
End Property

Public Property Let chunkSize(sz)
internalChunkSize = sz
End Property

'Calls Upload to extract the data from the binary request and then saves the uploaded files
Public Sub Save(path)
Dim streamFile, fileItem

if Right(path, 1) <> "\" then path = path & "\"

if not uploadedYet then Upload

For Each fileItem In UploadedFiles.Items
Set streamFile = Server.CreateObject("ADODB.Stream")
streamFile.Type = 1
streamFile.Open
StreamRequest.Position=fileItem.Start
StreamRequest.CopyTo streamFile, fileItem.Length
streamFile.SaveToFile path & fileItem.FileName, 2
streamFile.close
Set streamFile = Nothing
fileItem.Path = path & fileItem.FileName
Next
End Sub

public sub SaveOne(path, num, byref outFileName, byref outLocalFileName)
Dim streamFile, fileItems, fileItem, fs

set fs = Server.CreateObject("Scripting.FileSystemObject")
if Right(path, 1) <> "\" then path = path & "\"

if not uploadedYet then Upload
if UploadedFiles.Count > 0 then
fileItems = UploadedFiles.Items
set fileItem = fileItems(num)

outFileName = fileItem.FileName
outLocalFileName = GetFileName(path, outFileName)

Set streamFile = Server.CreateObject("ADODB.Stream")
streamFile.Type = 1
streamFile.Open
StreamRequest.Position = fileItem.Start
StreamRequest.CopyTo streamFile, fileItem.Length
streamFile.SaveToFile path & outLocalFileName, 2
streamFile.close
Set streamFile = Nothing
fileItem.Path = path & filename
end if
end sub

Public Function SaveBinRequest(path) ' For debugging purposes
StreamRequest.SaveToFile path & "\debugStream.bin", 2
End Function

Public Sub DumpData() 'only works if files are plain text
Dim i, aKeys, f
response.write "Form Items:<br>"
aKeys = FormElements.Keys
For i = 0 To FormElements.Count -1 ' Iterate the array
response.write aKeys(i) & " = " & FormElements.Item(aKeys(i)) & "<BR>"
Next
response.write "Uploaded Files:<br>"
For Each f In UploadedFiles.Items
response.write "Name: " & f.FileName & "<br>"
response.write "Type: " & f.ContentType & "<br>"
response.write "Start: " & f.Start & "<br>"
response.write "Size: " & f.Length & "<br>"
Next
End Sub

Public Sub Upload()
Dim nCurPos, nDataBoundPos, nLastSepPos
Dim nPosFile, nPosBound
Dim sFieldName, osPathSep, auxStr
Dim readBytes, readLoop, tmpBinRequest

'RFC1867 Tokens
Dim vDataSep
Dim tNewLine, tDoubleQuotes, tTerm, tFilename, tName, tContentDisp, tContentType
tNewLine = String2Byte(Chr(13))
tDoubleQuotes = String2Byte(Chr(34))
tTerm = String2Byte("--")
tFilename = String2Byte("filename=""")
tName = String2Byte("name=""")
tContentDisp = String2Byte("Content-Disposition")
tContentType = String2Byte("Content-Type:")

uploadedYet = true

on error resume next
readBytes = internalChunkSize
VarArrayBinRequest = Request.BinaryRead(readBytes)
VarArrayBinRequest = midb(VarArrayBinRequest, 1, lenb(VarArrayBinRequest))
for readLoop = 0 to 300000
tmpBinRequest = Request.BinaryRead(readBytes)
if readBytes < 1 then exit for
VarArrayBinRequest = VarArrayBinRequest & midb(tmpBinRequest, 1, lenb(tmpBinRequest))
next
if Err.Number <> 0 then
response.write "<br><br><B>System reported this error:</B><p>"
response.write Err.Description & "<p>"
response.write "The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the <A HREF='http://www.freeaspupload.net/freeaspupload/requirements.asp'>requirements page of freeaspupload.net</A>.<p>"
Exit Sub
end if
on error goto 0 'reset error handling

nCurPos = FindToken(tNewLine,1) 'Note: nCurPos is 1-based (and so is InstrB, MidB, etc)

If nCurPos <= 1 Then Exit Sub

'vDataSep is a separator like -----------------------------21763138716045
vDataSep = MidB(VarArrayBinRequest, 1, nCurPos-1)

'Start of current separator
nDataBoundPos = 1

'Beginning of last line
nLastSepPos = FindToken(vDataSep & tTerm, 1)

Do Until nDataBoundPos = nLastSepPos

nCurPos = SkipToken(tContentDisp, nDataBoundPos)
nCurPos = SkipToken(tName, nCurPos)
sFieldName = ExtractField(tDoubleQuotes, nCurPos)

nPosFile = FindToken(tFilename, nCurPos)
nPosBound = FindToken(vDataSep, nCurPos)

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

nCurPos = SkipToken(tFilename, nCurPos)
auxStr = ExtractField(tDoubleQuotes, nCurPos)
' We are interested only in the name of the file, not the whole path
' Path separator is \ in windows, / in UNIX
' While IE seems to put the whole pathname in the stream, Mozilla seem to
' only put the actual file name, so UNIX paths may be rare. But not impossible.
osPathSep = "\"
if InStr(auxStr, osPathSep) = 0 then osPathSep = "/"
oUploadFile.FileName = Right(auxStr, Len(auxStr)-InStrRev(auxStr, osPathSep))

if (Len(oUploadFile.FileName) > 0) then 'File field not left empty
nCurPos = SkipToken(tContentType, nCurPos)

auxStr = ExtractField(tNewLine, nCurPos)
' NN on UNIX puts things like this in the stream:
' ?? python py type=?? python application/x-python
oUploadFile.ContentType = Right(auxStr, Len(auxStr)-InStrRev(auxStr, " "))
nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line

oUploadFile.Start = nCurPos+1
oUploadFile.Length = FindToken(vDataSep, nCurPos) - 2 - nCurPos

If oUploadFile.Length > 0 Then UploadedFiles.Add LCase(sFieldName), oUploadFile
End If
Else
Dim nEndOfData
nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line
nEndOfData = FindToken(vDataSep, nCurPos) - 2
If Not FormElements.Exists(LCase(sFieldName)) Then
FormElements.Add LCase(sFieldName), Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
else
FormElements.Item(LCase(sFieldName))= FormElements.Item(LCase(sFieldName)) & ", " & Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
end if

End If

'Advance to next separator
nDataBoundPos = FindToken(vDataSep, nCurPos)
Loop
StreamRequest.WriteText(VarArrayBinRequest)
End Sub

Private Function SkipToken(sToken, nStart)
SkipToken = InstrB(nStart, VarArrayBinRequest, sToken)
If SkipToken = 0 then
Response.write "Error in parsing uploaded binary request. The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the <A HREF='http://www.freeaspupload.net/freeaspupload/requirements.asp'>requirements page of freeaspupload.net</A>.<p>"
Response.End
end if
SkipToken = SkipToken + LenB(sToken)
End Function

Private Function FindToken(sToken, nStart)
FindToken = InstrB(nStart, VarArrayBinRequest, sToken)
End Function

Private Function ExtractField(sToken, nStart)
Dim nEnd
nEnd = InstrB(nStart, VarArrayBinRequest, sToken)
If nEnd = 0 then
Response.write "Error in parsing uploaded binary request."
Response.End
end if
ExtractField = Byte2String(MidB(VarArrayBinRequest, nStart, nEnd-nStart))
End Function

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

'Byte string to string conversion
Private Function Byte2String(bsString)
Dim i
dim b1, b2, b3, b4
Byte2String =""
For i = 1 to LenB(bsString)
if AscB(MidB(bsString,i,1)) < 128 then
' One byte
Byte2String = Byte2String & ChrW(AscB(MidB(bsString,i,1)))
elseif AscB(MidB(bsString,i,1)) < 224 then
' Two bytes
b1 = AscB(MidB(bsString,i,1))
b2 = AscB(MidB(bsString,i+1,1))
Byte2String = Byte2String & ChrW((((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63)))
i = i + 1
elseif AscB(MidB(bsString,i,1)) < 240 then
' Three bytes
b1 = AscB(MidB(bsString,i,1))
b2 = AscB(MidB(bsString,i+1,1))
b3 = AscB(MidB(bsString,i+2,1))
Byte2String = Byte2String & ChrW(((b1 AND 15) * 16 + (b2 AND 60)) * 256 + (b2 AND 3) * 64 + (b3 AND 63))
i = i + 2
else
' Four bytes
b1 = AscB(MidB(bsString,i,1))
b2 = AscB(MidB(bsString,i+1,1))
b3 = AscB(MidB(bsString,i+2,1))
b4 = AscB(MidB(bsString,i+3,1))
' Don't know how to handle this, I believe Microsoft doesn't support these characters so I replace them with a "^"
'Byte2String = Byte2String & ChrW(((b1 AND 3) * 64 + (b2 AND 63)) & "," & (((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63)))
Byte2String = Byte2String & "^"
i = i + 3
end if
Next
End Function
End Class

Class UploadedFile
Public ContentType
Public Start
Public Length
Public Path
Private nameOfFile

' Need to remove characters that are valid in UNIX, but not in Windows
Public Property Let FileName(fN)
nameOfFile = fN
nameOfFile = SubstNoReg(nameOfFile, "\", "_")
nameOfFile = SubstNoReg(nameOfFile, "/", "_")
nameOfFile = SubstNoReg(nameOfFile, ":", "_")
nameOfFile = SubstNoReg(nameOfFile, "*", "_")
nameOfFile = SubstNoReg(nameOfFile, "?", "_")
nameOfFile = SubstNoReg(nameOfFile, """", "_")
nameOfFile = SubstNoReg(nameOfFile, "<", "_")
nameOfFile = SubstNoReg(nameOfFile, ">", "_")
nameOfFile = SubstNoReg(nameOfFile, "|", "_")
End Property

Public Property Get FileName()
FileName = nameOfFile
End Property

'Public Property Get FileN()ame
End Class


' Does not depend on RegEx, which is not available on older VBScript
' Is not recursive, which means it will not run out of stack space
Function SubstNoReg(initialStr, oldStr, newStr)
Dim currentPos, oldStrPos, skip
If IsNull(initialStr) Or Len(initialStr) = 0 Then
SubstNoReg = ""
ElseIf IsNull(oldStr) Or Len(oldStr) = 0 Then
SubstNoReg = initialStr
Else
If IsNull(newStr) Then newStr = ""
currentPos = 1
oldStrPos = 0
SubstNoReg = ""
skip = Len(oldStr)
Do While currentPos <= Len(initialStr)
oldStrPos = InStr(currentPos, initialStr, oldStr)
If oldStrPos = 0 Then
SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, Len(initialStr) - currentPos + 1)
currentPos = Len(initialStr) + 1
Else
SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, oldStrPos - currentPos) & newStr
currentPos = oldStrPos + skip
End If
Loop
End If
End Function

Function GetFileName(strSaveToPath, FileName)
'This function is used when saving a file to check there is not already a file with the same name so that you don't overwrite it.
'It adds numbers to the filename e.g. file.gif becomes file1.gif becomes file2.gif and so on.
'It keeps going until it returns a filename that does not exist.
'You could just create a filename from the ID field but that means writing the record - and it still might exist!
'N.B. Requires strSaveToPath variable to be available - and containing the path to save to
Dim Counter
Dim Flag
Dim strTempFileName
Dim FileExt
Dim NewFullPath
dim objFSO, p
Set objFSO = CreateObject("Scripting.FileSystemObject")
Counter = 0
p = instrrev(FileName, ".")
FileExt = mid(FileName, p+1)
strTempFileName = left(FileName, p-1)
NewFullPath = strSaveToPath & "\" & FileName
Flag = False

Do Until Flag = True
If objFSO.FileExists(NewFullPath) = False Then
Flag = True
GetFileName = Mid(NewFullPath, InstrRev(NewFullPath, "\") + 1)
Else
Counter = Counter + 1
NewFullPath = strSaveToPath & "\" & strTempFileName & Counter & "." & FileExt
End If
Loop
End Function

%>

Old Pedant
03-11-2010, 07:49 PM
Okay, looks like it can do it.

First thing I would do is put the <form> code and the form-processing code into separate files/pages.

The <form> could even be in a ".html" file instead of ASP.

You don't have to do this; it just simplifies life.

And the your mail-sending page becomes something like this:
<%@ Language=VBScript %>
<%
Response.Expires = -1
Server.ScriptTimeout = 600
%>
<!-- #include file="freeaspupload.asp" -->
<%
uploadsDirVar ="Path_to_my_Uploads_folder"

Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)
If Err.Number<>0 then
Response.Write "Fatal error in FreeASPUpload"
Response.End
End If

ks = Upload.UploadedFiles.keys
If UBound(ks) < 0 Then
Response.Write "The file name specified in the upload form does not correspond to a valid file in the system."
Response.End
End If

Response.Write "<B>Files uploaded:</B><ul>"
for each fileKey in Upload.UploadedFiles.keys
Response.Write "<li>" & Upload.UploadedFiles(fileKey).FileName _
& " (" & Upload.UploadedFiles(fileKey).Length & "B) </li>"
next

smtpserver = "IP address here"
youremail = "info@mysite.com"
yourpassword = "my_pwd"

ContactUs_Name = Upload.Form("ContactUs_Name")
ContactUs_Email = Upload.Form("ContactUs_Email")
ContactUs_Subject = Upload.Form("ContactUs_Subject")
ContactUs_Body = Upload.Form("ContactUs_Body")
Action = Upload.Form("Action")

' Check validity here and if no errors, then send the mail off

Set mail = CreateObject("CDO.Message")
...
for each fileKey in Upload.UploadedFiles.keys
mail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName
next
...
mail.Send
...

Old Pedant
03-11-2010, 07:49 PM
The important part being, of course, the use of Upload.Form in place of Request.

SteveH
03-12-2010, 12:03 PM
Hello Old Pedant

Thanks for your help.

I'll probably need to do this slowly so that I don't lose my way!

I'll break the form up into a) form and b) script, and will try to get the form part out of the way first. This is what I have now for my form after trying to incorporate the 'attachment' field to the other fields:


<form action="asp_script_behind_this_form.asp" method="POST" enctype="multipart/form-data" onSubmit="return onSubmitForm();">

<input type="hidden" name="Action" value="SendEmail">
<br>

<table border="0" cellspacing="1">
<tr><td valign="top">
Name:</td>
<td colspan="2">
<input type="text" name="ContactUs_Name" size="35" value="<% =ContactUs_Name %>">
</td></tr>


<tr>
<td valign="top">
Email:
</td>
<td colspan="2">
<input type="text" name="ContactUs_Email" size="35" value="<% =ContactUs_Email %>">
</td></tr>


<tr>
<td valign="top">
Subject:
</td>
<td colspan="2">
<input type="text" name="ContactUs_Subject" value="<% =ContactUs_Subject %>" size="35">
</td></tr>


<tr>
<td valign=top">
Attach:
</td>
<td colspan="2">

<input name="attach1" type="file" size=35>
<input style="margin-top:4" type=submit value="Upload">
</td></tr>


<tr>
<td valign="top">
Message:
</td>
<td valign="top">
<textarea rows="10" name="ContactUs_Body" cols="40"><% =ContactUs_Body %></textarea>
</td></tr>
<tr>
<td valign="top">
&nbsp;


</td>
<td colspan="2">
<input type="submit" value="Send">
</td></tr>
</table>
</form>

I have uploaded the form here (I will tidy it up later):

http://proofreading4students.com/Upload_Tests/Separate_form.html

What this means is that I am dropping the following from the original ASP upload form I had:

function OutputForm()

end function

Now, I can put all the other stuff in the main asp_script_behind_this_form.asp file. Is that right?

But what do I do with this part:

<HTML><HEAD>

JS stuff here

</HEAD>
<BODY>

<%
Dim diagnostics
if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
diagnostics = TestEnvironment()
if diagnostics<>"" then
response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
response.write diagnostics
response.write "<p>After you correct this problem, reload the page."
response.write "</div>"
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write "</div>"
end if
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write SaveFiles()
response.write "<br><br></div>"
end if

%>

</BODY>
</HTML>

Do I have that as a separate file, too? I am not even too sure what it does.

Thanks again for your help.

Steve

Old Pedant
03-12-2010, 07:27 PM
That diagnostic crap is only needed ONE TIME to verify that your server is capable of using the uploader. But if you *know* your system is usable, it's a waste of code.

Ignore it. The code will either work or not.

SteveH
03-13-2010, 11:42 AM
Hello Old Pedant

Thanks for that.

I am beginning to get somewhere (the attachment is now uploaded to my 'Uploads' folder of my server), but there are still some problems with it: emails are not getting through.

I get this error:

Files uploaded:
129.pdf (770743B)
Microsoft VBScript runtime error '800a01a8'

Object required: ''

/Upload_Tests/asp_script_behind_this_form.asp, line 55


Line 55 refers to this:

ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).

I'm not sure if that is a false error.

At the moment I have two ASP files:

a) separate_form.asp
b) asp_script_behind_this_form.asp

The code, which I know is a bit of a mess, behind the asp_script_behind_this_form.asp is as follows:

<%@ Language=VBScript %>
<%
Response.Expires = -1
Server.ScriptTimeout = 600
%>
<!-- #include file="freeaspupload.asp" -->
<%

Dim smtpserver,youremail,yourpassword,ContactUs_Name,ContactUs_Email
Dim ContactUs_Subject,ContactUs_Body,Action,IsError


uploadsDirVar ="path_to_Uploads"


Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)
If Err.Number<>0 then
Response.Write "Fatal error in FreeASPUpload"
Response.End
End If

ks = Upload.UploadedFiles.keys
If UBound(ks) < 0 Then
Response.Write "The file name specified in the upload form does not correspond to a valid file in the system."
Response.End
End If

Response.Write "<B>Files uploaded:</B><ul>"
for each fileKey in Upload.UploadedFiles.keys
Response.Write "<li>" & Upload.UploadedFiles(fileKey).FileName _
& " (" & Upload.UploadedFiles(fileKey).Length & "B) </li>"
next

smtpserver = "IP address"
youremail = "my_mail.com"
yourpassword = "my_pwd"


ContactUs_Name = Upload.Form("ContactUs_Name")
ContactUs_Email = Upload.Form("ContactUs_Email")
ContactUs_Subject = Upload.Form("ContactUs_Subject")
ContactUs_Body = Upload.Form("ContactUs_Body")
Action = Upload.Form("Action")

' Check validity here and if no errors, then send the mail off

Set mail = CreateObject("CDO.Message")

'This section provides the configuration information for the remote SMTP server.

ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = youremail
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = yourpassword

ObjSendMail.Configuration.Fields.Update

'End remote SMTP server configuration section==

ObjSendMail.To = youremail
ObjSendMail.Subject = ContactUs_Subject
ObjSendMail.From = ContactUs_Email
' we are sending a html email.. simply switch the comments around to send a text email instead
ObjSendMail.HTMLBody = strBody
'ObjSendMail.TextBody = strBody

ObjSendMail.Send

Set ObjSendMail = Nothing

' change the success messages below to say or do whatever you like
' you could do a response.redirect or offer a hyperlink somewhere.. etc etc
%>
<font size="2" color="navy"><center>Thank you for your message.</center>
<br><br>
<font color="blue">
<% =Replace(ContactUs_Body,vbCr,"<br>") %>
</font>
</font>


<form action="separate_form.asp" method="POST">


for each fileKey in Upload.UploadedFiles.keys
mail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName

next
else
SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
end if
end function
%>




I'm not sure if you are able to guide me through this.

Cheers, again, Pedant.

Steve

Old Pedant
03-14-2010, 06:00 AM
Ready to kick yourself?


Set mail = CreateObject("CDO.Message")

'This section provides the configuration information for the remote SMTP server.

ObjSendMail.Configuration. ....


Have to use the same name throughout. The name doesn't matter, but it must be the same.

SteveH
03-15-2010, 03:16 PM
Hello Old Pedant,

Thanks to you - almost there. The file is uploaded to the server and the email received by the webmaster.

I changed this:

Set mail = CreateObject("CDO.Message")

to this:

Set ObjSendMail = CreateObject("CDO.Message")

But do you know why I get this on the 'thank you' page:

Thank you for your message.

for each fileKey in Upload.UploadedFiles.keys mail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName next else SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system." end if end function %>

Thanks very much (again).

Steve

Old Pedant
03-15-2010, 06:26 PM
Sure...because that is supposed to be ASP code, but it's not inside the <%...%> tags.

And it's in the wrong place, anyway.

It needs to be *BEFORE* the SEND command.

So...


...
ObjSendMail.HTMLBody = strBody
'ObjSendMail.TextBody = strBody

' add the attachment(s)
for each fileKey in Upload.UploadedFiles.keys
objSendMail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName
next

ObjSendMail.Send

Set ObjSendMail = Nothing
%>
<font size="2" color="navy"><center>Thank you for your message.</center>
<br><br>
<font color="blue">
<% =Replace(ContactUs_Body,vbCr,"<br>") %>
</font>
</font>
</body>
</html>
<!-- this is the end of the page -->

The stuff in red is moved/corrected. The rest of the code is "as is" from your post.

Not sure it's right, but feels at least close.

SteveH
03-15-2010, 07:02 PM
Hello Old Pedant

The server does not like that, I'm afraid.

I get this error:

Files uploaded:
Separate_form.txt (1022B)
Microsoft VBScript runtime error '800a01a8'

Object required: ''

/Upload_Tests/asp_script_behind_this_form.asp, line 78

Line 78 is this:

mail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName

...which is a little odd, because it is exactly the same as before:confused:

Steve

Old Pedant
03-15-2010, 09:24 PM
Ummm....and it's the same reason.

You don't *HAVE* any object named "mail".

*YOUR* object is named "objSendMail".

And that *is* the name I used in the code in red in that post.

SteveH
03-15-2010, 09:58 PM
Sorry, I didn't see that the first time. This is what I now have after changing it:

for each fileKey in Upload.UploadedFiles.keys
ObjSendmail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName

next

ObjSendMail.Send

Set ObjSendMail = Nothing

I get this error:

Files uploaded:
ok.gif (470B)
CDO.Message.1 error '80070002'

The system cannot find the file specified.

/Upload_Tests/asp_script_behind_this_form.asp, line 78


Line 78 is that same line:

ObjSendmail.AddAttachment uploadsDirVar & Upload.UploadedFiles(fileKey).FileName

Sorry to bother you again

Steve

Old Pedant
03-16-2010, 12:34 AM
*MAYBE* you are just missing the \ between the directory name and the file name??

ObjSendmail.AddAttachment uploadsDirVar & "\" & Upload.UploadedFiles(fileKey).FileName

It would depend on whether or not your uploadsDirVar value ended with a \ or not. I usually put \ on the end of directory names, just in case. But if you didn't...

SteveH
03-16-2010, 10:41 AM
Hello Old Pedant

Originally, it looked like this:

fileName = uploadsDirVar & "\test.txt"

I changed the script to

ObjSendmail.AddAttachment uploadsDirVar & "\" & Upload.UploadedFiles(fileKey).FileName as you suggested and received an error (else SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system." end if end function %> ), so deleted this:

<form action="separate_form.asp" method="POST">

else
SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
end if
end function
%>

And now it seems to work just fine, so a thousands thanks to you!

Many thanks for everything you have done to make this work.

Steve