PDA

View Full Version : Uploading Photo help = Urgent


mckenzie03
10-08-2006, 12:44 AM
I am having trouble with my code - will not upload photo, can anyone please take a look and see what I have done wrong. This is urgent..

Here is the Form page :

----------------------
<!-- #include file = "admintop.asp" -->
<%
mode = Request.Form("txtMode")
intcatid = Request("catid")
if intcatid <> "" then
mode = "EDIT"
sSqlAgent = "select * from Agent_Master where Agent_id="&intcatid
set rsAgent = server.CreateObject("ADODB.Recordset")
rsAgent.Open sSqlAgent,oConnect
if not rsAgent.EOF then
strAgentname = rsAgent.Fields("Agent_Name")
strDesc = rsAgent.Fields("Agent_Image")
strEmail = rsAgent.Fields("Agent_Email")
strTitle = rsAgent.Fields("Agent_Title")
strPhone = rsAgent.Fields("Agent_Phone")
strDesc = rsAgent.Fields("Agent_Desc")

End if
End if
%>
<Script language=javascript src="../CommonFunctions/formvalidation.js"></Script>
<script language="javascript">
function callsubmit()
{
document.frmAgententry.imgUpload.value = "" ;
for(i=0;i<=document.forms['frmAgententry'].elements.length-1;i++ )
{
if(document.forms['frmAgententry'].elements[i].name=='txtimage1')
{
if(document.forms['frmAgententry'].elements[i].name=='txtimage1' && document.forms['frmAgententry'].elements[i].value != "")
{
document.frmAgententry.imgUpload.value = "1" //document.forms['frmAgententry'].elements[i].value ;
}
document.frmAgententry.action = "Agent_Submit.asp" ;
document.frmAgententry.submit();
}
function callcancel(){
document.frmAgententry.action = "Agent_List.asp" ;
document.frmAgententry.submit();
}
</script>
<form name=frmAgententry method=post enctype="multipart/form-data">
<div align="center">
<center>
<table border="0" width="95%" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td align="center"><font face="arial" size="3" color="#7B7B7B"><b>Add New Agent / Update Agent </b></font></td>
</tr>
<tr><td>&nbsp;</td></tr>
<tr>
<td valign="top" bgcolor="#BABABA">
<table border="0" width="100%" cellpadding="3" cellspacing="1">
<tr bgcolor="#ffffff">
<td width="26%" bgcolor="#DADADA"><div align="right">
<font face="Verdana" size="1" color="#000000"><b>New Agent</b> :</font></div></td>
<td width="74%" bgcolor="#DADADA">
<input type="text" name="txtAgentName" value="<%=strAgentname%>" size="20"></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA">
<p align="right"><b><font face="verdana" size="1" color="#000000">
Previous Image :</b></td>
<td bgcolor="#DADADA">
<div align="left">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400" id="AutoNumber1">
<tr>
<td><%if mode = "EDIT" then%>
<tr>
<td align="left">&nbsp;</td>
<td><img src="../images/Upload/<%=strAgent_Image%>" border=0 width=100></td>
</tr>
<%End if%>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA">
<p align="right"><strong><font face="Verdana" size="1">Agent
Photo :</font></strong></td>
<td bgcolor="#DADADA">
<input type="file" name="txtimage1" size="20"></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA"><div align="right"><strong>
<font face="Verdana" size="1">Email:</font></strong></div></td>
<td bgcolor="#DADADA">
<input name="txtEmail" type="text" id="txtEmail" value="<%=strEmail%>" size="20"></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA"><div align="right"><strong>
<font face="Verdana" size="1">Title:</font></strong></div></td>
<td bgcolor="#DADADA">
<input name="txtTitle" type="text" id="txtTitle" value="<%=strTitle%>" size="20"></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA"><div align="right"><strong>
<font face="Verdana" size="1">Phone:</font></strong></div></td>
<td bgcolor="#DADADA">
<input name="txtPhone" type="text" id="txtPhone" value="<%=strPhone%>" size="20"></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA">
<p align="right"><strong><font face="Verdana" size="1">Agents Bio:</font></strong></td>
<td bgcolor="#DADADA">
<textarea rows="5" name="txtDesc" cols="50"><%=strDesc%></textarea></td>
</tr>
<tr bgcolor="#ffffff">
<td bgcolor="#DADADA">&nbsp;</td>
<td bgcolor="#DADADA"><input type="button" value="Submit" name="B3" onClick="javascript:callsubmit();" style="background-color: #999999; color: #FFFFFF; font-family: Verdana; font-size: 7pt">
<input type="button" value="Cancel" onClick="javascript:callcancel();" name="B3" style="background-color: #999999; color: #FFFFFF; font-family: Verdana; font-size: 7pt"></td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</div>
<input type=hidden name="txtAgentId" value="<%=intcatid%>">
<input type=hidden name=imgUpload value=''>
<input type=hidden name="txtMode" value="<%=mode%>">
<INPUT type=hidden name=txtPrevImage1 size=30 value=<%=strImage_1%>>
</form>
<!-- #include file = "adminbottom.asp" -->---------------------------------------------
Here is the submit page
----------------------------
<!--#Include File="Agent_DB.asp"-->
<!--#Include File="../Connection.asp"-->
<!--#Include File="../Upload.asp"-->
<%
Set Uploader = new FileUploader
Uploader.Upload()
sCatFolder = "../images/Upload"
Agent_Id = Uploader.Form("txtAgentId")
mode = trim(ucase(Uploader.Form("txtMode")))
Agent_Name = Uploader.Form("txtAgentName")
Agent_Email = Uploader.Form("txtEmail")
Agent_Title = Uploader.Form("txtTitle")
Agent_Phone = Uploader.Form("txtPhone")
Agent_Desc = Uploader.Form("txtDesc")
' Agent_Image = Uploader.Form("txtMode")

if trim(ucase(mode)) ="NEW" or trim(ucase(mode)) ="EDIT" then
imgCount = trim(Uploader.form("imgUpload"))

if imgCount = "" then
imgCount = 0
end if
' Response.Write "imgCount="&imgCount&"<BR>"

fln1 = Uploader.form("txtPrevImage1")

for each file in Uploader.Files.Items
if sFile1 = "" then
sFile1 = file.FileName
end if
next

select case cint(imgCount)
case 0
Image_1 =fln1
Image_2 =fln2
Image_3 =fln3
Image_4 =fln4
case 1
Image_1=UploadImage(sFile1,fln1)
Image_2 =fln2
Image_3 =fln3
Image_4 =fln4
case 2
Image_1= fln1
Image_2 =UploadImage(sFile1,fln2)
Image_3 =fln3
Image_4 =fln4
case 3
Image_1 = fln1
Image_2 = fln2
Image_3 = UploadImage(sFile1,fln3)
Image_4 = fln4
case 4
Image_1 = fln1
Image_2 = fln2
Image_3 = fln3
Image_4 = UploadImage(sFile1,fln4)
case 12
Image_1=UploadImage(sFile1,fln1)
Image_2=UploadImage(sFile2,fln2)
Image_3 = fln3
Image_4 = fln4
case 13
Image_1=UploadImage(sFile1,fln1)
Image_2 = fln2
Image_3=UploadImage(sFile2,fln3)
Image_4 = fln4

case 14
Image_1=UploadImage(sFile1,fln1)
Image_2 = fln2
Image_3 = fln3
Image_4=UploadImage(sFile2,fln4)

case 23
Image_1 = fln1
Image_2=UploadImage(sFile1,fln2)
Image_3=UploadImage(sFile2,fln3)
Image_4 = fln4
case 24
Image_1 = fln1
Image_2 = UploadImage(sFile1,fln2)
Image_3 = fln2
Image_4 = UploadImage(sFile2,fln4)
case 123
Image_1=UploadImage(sFile1,fln1)
Image_2=UploadImage(sFile2,fln2)
Image_3=UploadImage(sFile3,fln3)
Image_4 = fln4
case 1234
Image_1=UploadImage(sFile1,fln1)
Image_2=UploadImage(sFile2,fln2)
Image_3=UploadImage(sFile3,fln3)
Image_4=UploadImage(sFile4,fln4)
end select

end if

Select Case ucase(trim(mode))
case "NEW"
success = Agent_Master_Update(Agent_Id,Agent_Name,Agent_Email,Agent_Title,Agent_Phone,Agent_Desc,Agent_Image)
case "EDIT"
success = Agent_Master_Update(Agent_Id,Agent_Name,Agent_Email,Agent_Title,Agent_Phone,Agent_Desc,Agent_Image)
End Select

if success then
Response.Redirect "Agent_List.asp?MsgId=1"
else
Response.Redirect "Agent_List.asp?MsgId=2"
end if



function UploadImage(ImagePath,ImagePathPrev)
blnExist = "false"
sCatFolder = "../images/Upload"
sURl = ""
sFile = ImagePath
on error resume next
If sFile <> "" then
'check if image file already exist
sURL = sCatFolder & sFile
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If not oFS.FileExists (Server.MapPath(sURL)) then
UploadedFileName = UploadDocument(ImagePath)
else
'else set flag to flash image file exist message'
strMsg =" Image File already exists."
blnExist= "true"
end if

if trim(ImagePathPrev) <> "" then
Set oFS1 = Server.CreateObject("Scripting.FileSystemObject")
If oFS1.FileExists(Server.MapPath(ImagePathPrev)) Then
'Response.Write Server.MapPath(Trim(ImagePathPrev))
oFS1.DeleteFile Server.MapPath(ImagePathPrev),True
End If
set oFS1 = nothing
end if
set oFS = nothing
Else
UploadedFileName = ""
End if
UploadImage = UploadedFileName
if err.number > 0 then
Response.Write err.Description
Response.End
end if
end function


function UploadDocument(ImagePath)
PhysicalPath = Server.MapPath(sCatFolder)
DestinationPath = PhysicalPath
LogFolder = PhysicalPath
For Each File In Uploader.Files.Items
if trim(ImagePath) = File.filename then
sImageFile = File.SaveToDisk(DestinationPath)
end if
next
UploadDocument = sImageFile
End function

%>
-----------------------------------
and here is my DB page
-----------------------------------
<%
'''The Class Starts Here
'*******************************************************************************************
' Page :
' Purpose :
' Author :
' Date :
'*******************************************************************************************
'*******************************************************************************************
''' Insert Function
'*******************************************************************************************
Function Agent_Master_Insert(Agent_Name,Agent_Email,Agent_Title,Agent_Phone,Agent_Desc,Agent_Image)

On Error Resume Next

strInsert="Insert Into Agent_Master(Agent_Name,Agent_Email,Agent_Title,Agent_Phone,Agent_Desc,Agent_Image) values ("
strInsert=strInsert & "'"& Agent_Name &"', '" & Agent_Email & "', '" & Agent_Title & "', '" & Agent_Phone & "', '" & Agent_Desc & "'"
strInsert=strInsert & "'" & Agent_Image & "',"
strInsert=strInsert & ")"
strInsert=replace(strInsert,",)",")")
'response.Write(strInsert)
'response.End()
''' Executing the statement
' oConnect.Execute strInsert,Agent_Master_Insert

dim objcmd, RecordsAffected
set objcmd = server.CreateObject("ADODB.Command")
objcmd.ActiveConnection = oConnect
objcmd.CommandText = strInsert
objcmd.CommandType = adCmdText
objcmd.Execute RecordsAffected
If RecordsAffected >=1 then
Agent_Master_Insert = True
else
Agent_Master_Insert = False
end if

End Function

'*******************************************************************************************
''' modify Function
'*******************************************************************************************
Function Agent_Master_Update(Agent_Id,Agent_Name,Agent_Email,Agent_Title,Agent_Phone,Agent_Desc,Agent_Image)
On Error Resume Next

strUpdate="UpDate Agent_Master Set "
strUpdate=strUpdate & "Agent_Name = '" & Agent_Name & "', Agent_Email = '" & Agent_Email & "', "
strUpdate=strUpdate & "Agent_Title = '" & Agent_Title & "', Agent_Phone = '" & Agent_Phone & "', Agent_Desc = '" & Agent_Desc & "'"
strUpdate=strUpdate & "'" & Agent_Image & "',"
strUpdate=strUpdate& " where Agent_Id = " & Agent_Id
strUpdate=replace(strUpdate,", where"," where")
'response.Write(strUpdate)
'response.End()
dim objcmd, RecordsAffected
set objcmd = server.CreateObject("ADODB.Command")
objcmd.ActiveConnection = oConnect
objcmd.CommandText = strUpdate
objcmd.CommandType = adCmdText
objcmd.Execute RecordsAffected
If RecordsAffected >=1 then
Agent_Master_Update = True
else
Agent_Master_Update = False
end if

End Function

'*******************************************************************************************
''' Delete Function
'*******************************************************************************************
Function Agent_Master_Delete(Agent_Id)
On Error Resume Next

''' Check whether record is available to delete
set rsDel = Server.CreateObject("ADODB.Recordset")
rsDel.open "Select * from Agent_Master where Agent_Id = " & Agent_Id, oConnect
if not rsdel.eof and not rsdel.bof then

''' delete statement with respect to Primary key selected
strDelete="Delete From Agent_Master where Agent_Id = " & Agent_Id
dim objcmd, RecordsAffected
set objcmd = server.CreateObject("ADODB.Command")
objcmd.ActiveConnection = oConnect
objcmd.CommandText = strDelete
objcmd.CommandType = adCmdText
objcmd.Execute RecordsAffected
If RecordsAffected >=1 then
Agent_Master_Delete = True
else
Agent_Master_Delete = False
end if
else
Agent_Master_Delete = True
end if

End Function



%>
-------------------------------
I am new to this and im in desperate need of some help - can some one please tell me what I have done wrong - Ill post the upload script im using also


Thanks you for all your help........

McKenzie

mckenzie03
10-08-2006, 12:45 AM
------------------------------------------
Here is the upload script
---------------------------
<%
'***************************************
' 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
Public 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()
' Response.Write "in form 3"
Set mcolFormElem = Nothing
End If
End Sub

Public Property Get Form(sIndex)
Form = ""
'Response.Write "in form 2"
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
'Response.Write "in form"
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 function SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex
Dim strFileName

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

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

'Make Modification for a Unique File Name

'Getting Current Date & Time
strFileName = replace(date(),"/","") & replace(time(),":","")
'Response.Write strFileName
'Removing blank space
strFileName = cdbl(trim(mid(strFileName,1,instr(1,strFileName," "))))

'getting numeric data from file name
numFilename = strFileName

'Attaching extention
strFileName = strFileName & mid(FileName,instr(1,FileName,"."))

'Response.Write strFileName
FileName = "F" & strFileName

'Check duplicate file name
'If exist then change its name by showing one incremented value.
if oFS.FileExists(sPath & FileName) then
'numFilename = cdbl(numFilename) + 1
numFilename = cstr(numFilename) + "_1"
strFileName = numFilename & mid(FileName,instr(1,FileName,"."))
FileName = "F" & strFileName
end if

if oFS.FileExists(sPath & FileName) then
'numFilename = cdbl(numFilename) + 1
numFilename = cstr(numFilename) + "_1"
strFileName = numFilename & mid(FileName,instr(1,FileName,"."))
FileName = "F" & strFileName
end if
if oFS.FileExists(sPath & FileName) then
'numFilename = cdbl(numFilename) + 1
numFilename = cstr(numFilename) + "_1"
strFileName = numFilename & mid(FileName,instr(1,FileName,"."))
FileName = "F" & strFileName
end if

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

For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next
SaveToDisk = FileName
oFile.Close
End function

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
%>

degsy
10-09-2006, 02:16 PM
You haven't given any indication to what the problem is :rolleyes:

mckenzie03
10-10-2006, 09:45 PM
Im not sure what the problem is - It just keeps failing to upload .. I have tried everything. Started from scratch and still nothing...... Some one please help. I will be willing to pay some one if they can write the code over for me. I will give them the access to the full site.... Some one please Help...:eek:

maw
10-11-2006, 11:57 AM
Do you get an error when you try to upload or does the code execute as expected?