Shadow Uploader - using GetFileIndexByName with non IE browsers
Discuss Shadow Uploader - using GetFileIndexByName with non IE browsers in the ASP Development forum on ASP Free. Shadow Uploader - using GetFileIndexByName with non IE browsers ASP Development forum discussing ASP related topics including coding practices, ASP tips, and more. Active Server Pages (ASP) enables you to empower your HTML pages dynamically with robust scripting options.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 31,109
Time spent in forums: 3 Months 3 Weeks 2 Days 20 h 18 m 8 sec
Reputation Power: 2919
Quote:
Originally Posted by Computer-bot
I am Getting an error saying that
Error Type:
Microsoft VBScript runtime (0x800A0009)
Subscript out of range: 'index'
I get this error when I include the 2nd line fro the following two lines.
fileIndex = GetFileIndexByName(objUpload, "c_pic_real")
PhotoExtension=objUpload.File(fileIndex).contentTy pe
Any Idea????
CB
yep - you didn't use any file input named "c_pic_real" or you maybe have it, but uploaded no file
using it. empty file inputs won't result in file.
you should always use sanity check to avoid such errors:
Code:
fileIndex = GetFileIndexByName(objUpload, "c_pic_real")
If fileIndex>=0 And fileIndex<=objUpload.FileCount Then
PhotoExtension = objUpload.File(fileIndex).contentType
Else
'no such file, nothing to do
End If
if you have one file only, use this code instead no need for using name:
Code:
If objUpload.FileCount=1 Then
PhotoExtension = objUpload.File(0).contentType
End If
Posts: 459
Time spent in forums: 3 Days 19 h 55 m 38 sec
Reputation Power: 26
Code:
<%
Function GetFileIndexByName(objUpload, strName)
Dim x, curName, strToSearch
For x=0 To objUpload.FileCount-1
curName = LCase(objUpload.File(x).FileName)
strToSearch = "\" & curName
If Right(LCase(objUpload(strName)), Len(strToSearch))=strToSearch Then
GetFileIndexByName = x
Exit Function
End If
Next
GetFileIndexByName = -1
End Function
%>
<head>
<!--#include file="../include/File.asp"-->
<script type="text/javascript">
function StoreFileNames(objForm) {
for (var i = 0; i < objForm.elements.length; i++) {
var element = objForm.elements[i];
if (element.type == "file")
objForm.elements[element.name + "_real"].value = element.value;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add_Channels</title>
</head>
<body>
<% If Request.ServerVariables("REQUEST_METHOD") <> "POST" then %>
<!-- METADATA TYPE="typelib" FILE="c:\Program Files\Common Files\System\ado\msado15.dll" -->
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="737" border="0">
<tr>
<th width="224" scope="col"><div align="left">Channel Name </div></th>
<th width="503" scope="col"><div align="left">
<input name="C_Name" type="text" id="C_Name" tabindex="1" />
</div></th>
</tr>
<tr>
<th valign="top" scope="row"><div align="left">Channel Description </div></th>
<td><div align="left">
<textarea name="C_Desc" cols="50" rows="7" id="C_Desc" tabindex="2"></textarea>
</div></td>
</tr>
<tr>
<th scope="row"><div align="left">Channel Display Picture</div></th>
<td><div align="left">
<input name="c_pic" type="file" tabindex="3" /></div></td>
<tr> <input type="hidden" name="c_pic_real" /> </tr>
<tr>
<th scope="row"><div align="left">Please Type the Code </div></th>
<td><input name="Captcha" type="text" tabindex="4" id="Captcha" /><img src="../include/aspcaptcha.asp" alt="This Is CAPTCHA Image" width="86" height="21" /></td>
</tr>
<tr>
<th colspan="2" scope="row"><div align="left">
<input name="C_Submit" type="submit" id="C_Submit" tabindex="5" value="Submit" />
</div>
<div align="left"></div></th>
</tr>
</table>
</form>
<%
Else
Set objUpload = new ShadowUpload
C_Captcha = objUpload("Captcha")
' If CheckCAPTCHA(C_Captcha) = False Then
' Response.write("Wrong Code Entered")
' Else
Dim C_Name
Dim C_Desc
Dim C_Captcha
Dim C_TempID
Dim P_TempID
Dim ValRand
Dim Channel_Folder
Highest=32767
Lowest=10
'################################ CONNECTION AND RECORDSET INITIATION ################################################
Set objConn1 = Server.CreateObject("ADODB.Connection")
Set objRs1 = Server.Createobject("ADODB.Recordset")
Set objRs2 = Server.CreateObject("ADODB.Recordset")
objConn1.Open strConnect
objRs1.Open "Select * From Channels" , ObjConn1
objRs2.Open "Select channel_folder from Meta_Info", objConn1
'################################################# ################################################## #################
'############################################ Creating Random Values for Channel_ID #################################
strSQL1 = "Select COUNT(*) As MyCount From Channels"
TRecords = objConn1.Execute(strSQL1)
If TRecords("MyCOunt") <> "0" Then
ObjRs1.MoveFirst
Randomize
C_TempID = objRs1.Fields("Channel_ID")
For x=1 To 50
Val_Rand = CStr(CLng((Highest-Lowest + 1) * Rnd + Lowest))
If Val_Rand < 32767 and Val_Rand > 10 Then Exit For
Next
Do
If Val_Rand <> C_TempID Then
objRs1.MoveNext
If objRs1.EOF = False Then C_TempID = objRs1.Fields("Channel_ID")
Else
objRs1.MoveFirst
Randomize
For x=1 To 50
Val_Rand = CStr(CLng((Highest-Lowest + 1) * Rnd + Lowest))
If Val_Rand < 32767 and Val_Rand > 10 Then Exit For
Next
C_TempID = objRs1.Fields("Channel_ID")
End If
Loop While objRs1.EOF = False
End If
'################################################# ################################################## #######################
'############################# Generating Random Value for Picture Name and Uploading the Picture #########################
strSQL1 = "Select COUNT(*) As MyCount From Channels"
TRecords = objConn1.Execute(strSQL1)
If TRecords("MyCOunt") <> "0" Then
ObjRs1.MoveFirst
Randomize
P_TempID = objRs1.Fields("Channel_Pic")
P_TempID = Left(P_TempID, InStrRev(P_TempID, ".")-1)
For x=1 To 50
Val_Rand2 = CStr(CLng((Highest-Lowest + 1) * Rnd + Lowest))
If Val_Rand2 < 32767 and Val_Rand > 10 Then Exit For
Next
Do
If Val_Rand2 <> P_TempID Then
objRs1.MoveNext
If objRs1.EOF = False Then
P_TempID = objRs1.Fields("Channel_Pic")
P_TempID = Left(P_TempID, InStrRev(P_TempID, ".")-1)
End If
Else
objRs1.MoveFirst
Randomize
For x=1 To 50
Val_Rand2 = CStr(CLng((Highest-Lowest + 1) * Rnd + Lowest))
If Val_Rand2 < 32767 and Val_Rand > 10 Then Exit For
Next
P_TempID = objRs1.Fields("Channel_Pic")
P_TempID = Left(P_TempID, InStrRev(P_TempID, ".")-1)
End If
Loop While objRs1.EOF = False
End If
'Up till here we have a unique name to be added in the database
'Now we need to check whether this name along with the extention
'exists in the folder containing the channel images.
set fso = Server.CreateObject("Scripting.FileSystemObject") 'Extracting Picture Extension
fileIndex = GetFileIndexByName(objUpload, "c_pic")
PhotoExtension=objUpload.File(fileindex).contentTy pe
T_Length=Len(PhotoExtension)
Ext_Length=InStrRev(PhotoExtension, "/")
PhotoExtension=Right(PhotoExtension, (T_Length-Ext_Length))
File_Name=(CStr(Val_Rand2&"."&PhotoExtension))
If (fso.FileExists(Server.Mappath(adFolder+File_Name) )=True) then 'Changing Picture Name if It exists in the image folder
Do
For x=1 To 50
Val_Rand2=CStr(CLng((Highest-Lowest+1)*Rnd+Lowest))
If Val_Rand2<32767 and Val_Rand3>10 Then Exit For
Next
Loop While (fso.FileExists(Server.Mappath(adFolder+File_Name) )=True)
File_Name=(CStr(Val_Rand2&"."&PhotoExtension))
End If
Channel_Folder = objRs2.Fields("channel_folder")
Call objUpload.File(0).SaveToDisk(server.mappath(Channe l_Folder), File_Name )
'################################################# ################################################## #######################
C_Name = objUpload("C_Name") 'Assigning vales from form to variables.
C_Desc = objUpload("C_Desc")
C_ID = Val_Rand
C_Pic= File_Name
Set objComm = Server.CreateObject("ADODB.Command")
objComm.ActiveConnection = strConnect
objComm.CommandText = "Update_Channels"
objComm.CommandType = adCmdStoredProc
Set objParam1 = objComm.CreateParameter("@Channel_ID", adinteger, adParamInput)
Set objParam2 = objComm.CreateParameter("@Channel_Name", adVarChar, adParamInput, 50)
Set objParam3 = objComm.CreateParameter("@Channel_Desc", adVarChar, adParamInput, 200)
Set objParam4 = objComm.CreateParameter("@Channel_Pic", adVarChar, adParamInput, 20)
objComm.Parameters.Append objParam1
objComm.Parameters.Append objParam2
objComm.Parameters.Append objParam3
objComm.Parameters.Append objParam4
objComm.Parameters("@Channel_ID") = C_ID
objComm.Parameters("@Channel_Name") = C_Name
objComm.Parameters("@Channel_Desc") = C_Desc
objComm.Parameters("@Channel_Pic") = C_Pic
Response.write("OK")
'Set objRs = objComm.Execute
Set objComm = Nothing
Set objRs = Nothing
'################################################# ################################################## #################
Response.Redirect("channel_confirm.asp")
'End If
End If%>
</body>
</html>
Look for the boldface lines above.
Thanks in advance
CB
Last edited by Shadow Wizard : March 23rd, 2009 at 10:41 AM.
Reason: added code tags
Posts: 31,109
Time spent in forums: 3 Months 3 Weeks 2 Days 20 h 18 m 8 sec
Reputation Power: 2919
please read my first reply today. it's post #68.
if you can't find it let me know and I'll post it again or send you by private message.
in that post I added "sanity check" code. you don't have this in your code.
add that code (that I have posted) to your existing code and you won't get error.
also, if you have only one file I can't see the reason you insist on using the name..
you should still check to see if any file was uploaded though by checking
the Count and verifying it's more than 0.
last but not least, when posting code in this forum use code tags. it's very simple
to add and it improve readability of the code greatly. if you don't know how to
add code tags please send me PM (private message) and I'll try to explain there.
Posts: 459
Time spent in forums: 3 Days 19 h 55 m 38 sec
Reputation Power: 26
Sorry for not usng the code tags. Will be careful next time. As you said, I've added the sanity check - I was going to ad it later anyway... but still sanity check doesn't solve the problem. The problem is that I receive the Fie not found message even when I choose the file. I've found a way around and want to share it with you. In the function GetFileIndexByName, When I remove the boldface part below, the code works just fine.
Code:
Function GetFileIndexByName(objUpload, strName)
Dim x, curName, strToSearch
For x=0 To objUpload.FileCount-1
curName = LCase(objUpload.File(x).FileName)
strToSearch = "\" & curName
If Right(LCase(objUpload(strName)), Len(strToSearch))=strToSearch Then
GetFileIndexByName = x
Exit Function
End If
Next
GetFileIndexByName = -1
End Function
But with this change, uploading a picture becomes mandatory. Or I get the same error. Subscript out of range: 'index'
Posts: 31,109
Time spent in forums: 3 Months 3 Weeks 2 Days 20 h 18 m 8 sec
Reputation Power: 2919
removing the GetFileIndexByName = -1 from the function will simply
cause it will return "default" value of 0. there's no point in the function
if you do this.
forgot about StoreFileNames function, sorry - my mistake thanks
for letting us know.
Posts: 459
Time spent in forums: 3 Days 19 h 55 m 38 sec
Reputation Power: 26
Alright there is one more issue. How would I determine whether the file selected is an image. I am using the codeEnd If
Code:
If (objUpload.File(fileIndex).ImageWidth <> -1) Then
Upload the Picture
Else
Generate error message
This code works perfect for .doc and .txt files but doesn't work with .flv, .mpg and other such files. It doesn't show the error. the page says that THE WEB PAGE IS NOT AVAILABLE.