Forums
This topic is locked
Pure asp Upload problems
Posted 21 May 2002 09:59:35
1
has voted
21 May 2002 09:59:35 Giampaolo Frongia posted:
Hi, i have created an insert form with UD4 .Simultanouesly i can send a message for a newsletter with CDONTS, but when apply the Pure ASp Upload 1.5, the newsletter doesn't work. Can you help me please?
There is the code
<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../../Connections/connPRODUZIONE.asp" -->
<%
'*** File Upload to: ../files, Extensions: "", Form: CDO_Email_Form, Redirect: ""
'*** Pure ASP File Upload -----------------------------------------------------
' Copyright 2000 (c) George Petrov
'
' Script partially based on code from Philippe Collignon
' (www.asptoday.com/articles/20000316.htm)
'
' New features from GP:
' * Fast file save with ADO 2.5 stream object
' * new wrapper functions, extra error checking
' * UltraDev Server Behavior extension
'
' Version: 1.5.0
'------------------------------------------------------------------------------
Sub BuildUploadRequest(RequestBin)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--"

'Members variable of objects are put in a dictionary object
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary"

'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"

Pos = InstrB(Pos,RequestBin,getByteString("name="

PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="

PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
FileName = Mid(FileName,InStrRev(FileName,"\"

'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"

PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = FileName
ValueBeg = PosBeg-1
ValueLen = PosEnd-Posbeg
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
ValueBeg = 0
ValueEnd = 0
End If
'Add content to dictionary object
UploadControl.Add "Value" , Value
UploadControl.Add "ValueBeg" , ValueBeg
UploadControl.Add "ValueLen" , ValueLen
'Add dictionary object to main dictionary
UploadRequest.Add name, UploadControl
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End Sub
'String to byte string conversion
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function
'Byte string to string conversion
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
Function UploadFormRequest(name)
on error resume next
if UploadRequest.Item(name) then
UploadFormRequest = UploadRequest.Item(name).Item("Value"

end if
End Function
'Process the upload
UploadQueryString = Replace(Request.QueryString,"GP_upload=true",""

if mid(UploadQueryString,1,1) = "&" then
UploadQueryString = Mid(UploadQueryString,2)
end if
GP_uploadAction = CStr(Request.ServerVariables("URL"

If (Request.QueryString <> ""

if UploadQueryString <> "" then
GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
end if
End If
If (CStr(Request.QueryString("GP_upload"


GP_redirectPage = ""
If (GP_redirectPage = ""

GP_redirectPage = CStr(Request.ServerVariables("URL"

end if
RequestBin = Request.BinaryRead(Request.TotalBytes)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary"

BuildUploadRequest RequestBin
GP_keys = UploadRequest.Keys
for GP_i = 0 to UploadRequest.Count - 1
GP_curKey = GP_keys(GP_i)
'Save all uploaded files
if UploadRequest.Item(GP_curKey).Item("FileName"

GP_value = UploadRequest.Item(GP_curKey).Item("Value"

GP_valueBeg = UploadRequest.Item(GP_curKey).Item("ValueBeg"

GP_valueLen = UploadRequest.Item(GP_curKey).Item("ValueLen"

if GP_valueLen = 0 then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName"

Response.Write "File does not exists or is empty.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
response.End
end if
'Create a Stream instance
Dim GP_strm1, GP_strm2
Set GP_strm1 = Server.CreateObject("ADODB.Stream"

Set GP_strm2 = Server.CreateObject("ADODB.Stream"

'Open the stream
GP_strm1.Open
GP_strm1.Type = 1 'Binary
GP_strm2.Open
GP_strm2.Type = 1 'Binary
GP_strm1.Write RequestBin
GP_strm1.Position = GP_ValueBeg
GP_strm1.CopyTo GP_strm2,GP_ValueLen
'Create and Write to a File
GP_curPath = Request.ServerVariables("PATH_INFO"

GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/"


if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
GP_curPath = GP_curPath & "/"
end if
on error resume next
GP_strm2.SaveToFile Trim(Server.mappath(GP_curPath))& "\" & UploadRequest.Item(GP_curKey).Item("FileName"

if err then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName"

Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
err.clear
response.End
end if
end if
next
'*** GP NO REDIRECT
end if
if UploadQueryString <> "" then
UploadQueryString = UploadQueryString & "&GP_upload=true"
else
UploadQueryString = "GP_upload=true"
end if
%>
<%
Dim rsMAILING__MMColParam
rsMAILING__MMColParam = "1"
if (Request.Form("chimica_gomma_plastica"



%>
<%
Dim rsMAILING__varMec
rsMAILING__varMec = "1"
if (Request.Form("meccanica_elettronica"



%>
<%
Dim rsMAILING__varLegno
rsMAILING__varLegno = "1"
if (Request.Form("legno_arredamento"



%>
<%
Dim rsMAILING__varMultimedia
rsMAILING__varMultimedia = "1"
if (Request.Form("hitech"



%>
<%
Dim rsMAILING__varSanitarie
rsMAILING__varSanitarie = "1"
if (Request.Form("sanitarie"



%>
<%
Dim rsMAILING__varOdonto
rsMAILING__varOdonto = "1"
if (Request.Form("odontotecnici"



%>
<%
Dim rsMAILING__varMedicale
rsMAILING__varMedicale = "1"
if (Request.Form("medicale"



%>
<%
set rsMAILING = Server.CreateObject("ADODB.Recordset"

rsMAILING.ActiveConnection = MM_connPRODUZIONE_STRING
rsMAILING.Source = "SELECT EMAIL FROM dbo.ACCESSO WHERE [CHIMICA - GOMMA - PLASTICA] = '" + Replace(rsMAILING__MMColParam, "'", "''"







rsMAILING.CursorType = 0
rsMAILING.CursorLocation = 2
rsMAILING.LockType = 3
rsMAILING.Open()
rsMAILING_numRows = 0
%>
<%
' *** Edit Operations: (Modified for File Upload) declare variables
MM_editAction = CStr(Request.ServerVariables("URL"


If (UploadQueryString <> ""

MM_editAction = MM_editAction & "?" & UploadQueryString
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: (Modified for File Upload) set variables
If (CStr(UploadFormRequest("MM_insert"


MM_editConnection = MM_connPRODUZIONE_STRING
MM_editTable = "dbo.produzione"
MM_editRedirectUrl = "inserok_estero.asp"
MM_fieldsStr = "meccanica_elettronica|value|visibile2|value|nazionale2|value|pubblico2|value|chimica_gomma_plastica|value|legno_arredamento|value|hitech|value|sanitarie|value|medicale|value|odontotecnici|value|andamento_settore|value|normative|value|progetti_imprese|value|formazione|value|servizi|value|iniziative_commerciali|value|relazioni_sindacali|value|tipo_appuntamento|value|luogo_appuntamento|value|mese|value|data|value|varSubject|value|file|value|varMessage|value"
MM_columnsStr = "meccanica_elettronica|',none,''|visibile|',none,''|nazionale|',none,''|pubblico|',none,''|chimica_gomma_plastica|',none,''|legno_arredamento|',none,''|hitech|',none,''|sanitarie|',none,''|medicale|',none,''|odontotecnici|',none,''|andamento_settore|',none,''|normative|',none,''|progetti_imprese|none,'Y','N'|formazione|',none,''|servizi|',none,''|iniziative_commerciali|none,'Y','N'|relazioni_sindacali|',none,''|tipo_appuntamento|',none,''|luogo_appuntamento|',none,''|mese|',none,''|data|',none,''|titolo|',none,''|documento|',none,''|circolare|',none,''"
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|"

MM_columns = Split(MM_columnsStr, "|"

' set the form values
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(i+1) = CStr(UploadFormRequest(MM_fields(i)))
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And UploadQueryString <> ""

If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And UploadQueryString <> ""

MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString
End If
End If
End If
%>
<%
' *** Insert Record: (Modified for File Upload) construct a sql insert statement and execute it
If (CStr(UploadFormRequest("MM_insert"


' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For i = LBound(MM_fields) To UBound(MM_fields) Step 2
FormVal = MM_fields(i+1)
MM_typeArray = Split(MM_columns(i+1),","

Delim = MM_typeArray(0)
If (Delim = "none"

AltVal = MM_typeArray(1)
If (AltVal = "none"

EmptyVal = MM_typeArray(2)
If (EmptyVal = "none"

If (FormVal = ""

FormVal = EmptyVal
Else
If (AltVal <> ""

FormVal = AltVal
ElseIf (Delim = "'"

FormVal = "'" & Replace(FormVal,"'","''"

Else
FormVal = Delim + FormVal + Delim
End If
End If
If (i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End if
MM_tableValues = MM_tableValues & MM_columns(i)
MM_dbValues = MM_dbValues & FormVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & "


If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command"

MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
Dim Repeat1__numRows
Repeat1__numRows = -1
Dim Repeat1__index
Repeat1__index = 0
rsMAILING_numRows = rsMAILING_numRows + Repeat1__numRows
While ((Repeat1__numRows <> 0) AND (NOT rsMAILING.EOF))
' CDO Email Form v1.0
' Pete Erwin www.assonetriver.com
If (cStr(Request("Submit"


Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail"

objCDO.From = " " 'Senders Email Address
objCDO.To = (rsMAILING.Fields.Item("EMAIL"

objCDO.Subject = cStr(Request("varSubject"

objCDO.Body = cStr(Request("varMessage"

objCDO.Send() 'send mail
Set objCDO = Nothing 'Clean up your objects!!!
End If
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rsMAILING.MoveNext()
Wend
Response.Redirect("insertok.asp"

End If
End If
%>
Rebel
Replies
Replied 17 Jul 2002 18:40:40
17 Jul 2002 18:40:40 Alan Chua replied:
hi, seems that I have the same problem here. I created a form with user id name and address field, everything works fine but when i include a file field, and apply the asp upload server behaviour, everything doesnt work anymore
I have error message like this :
"Request object error 'ASP 0208 : 80004005'
Cannot use generic Request collection
/user_reg/validate.asp, line 264
Cannot use the generic Request collection after calling BinaryRead. "
Wonder you have solve your problem already. If you have, can you help me?
Thanks in advance
Regards,
Lonetree
I have error message like this :
"Request object error 'ASP 0208 : 80004005'
Cannot use generic Request collection
/user_reg/validate.asp, line 264
Cannot use the generic Request collection after calling BinaryRead. "
Wonder you have solve your problem already. If you have, can you help me?
Thanks in advance
Regards,
Lonetree