Uploader.Form (getting the name of the file)

Hi,

I am using the "Pure ASP File Upload" from ASP 101 (http://www.asp101.com/articles/jacob/scriptupload.asp) and I cannot seem to get the name of my file from my form. It's not throwing an error, it's just not returning anything.

This is how the form element looks:

     <input type="File" size="50" name="FILE1" class="FormInputFile">

And this is the code I am using to call it:

     FileExtension=Uploader.Form("FILE1")
     FileExtension = Ucase(Right(FileExtension,4))

I am really trying to get the file extension of the file that is being uploaded, because I want to limit the types that can be uploaded.

Does anyone have any ideas what I may be doing wrong?

Thanks,

Tammi
okcuserAsked:
Who is Participating?
 
GaryCommented:
Try this

    If Uploader.Files.Count = 0 Then
          Response.Redirect("moc_attachments.asp?varMOCID="& varMOCID &"&FromAttach=Yes&FileAdded=No")
     Else
      For Each File In Uploader.Files.Items
          MAXSIZE = 2000000
          FileSize=File.FileSize
         
          If FileSize > MAXSIZE Then
               Response.Redirect("moc_attachments.asp?varMOCID="& varMOCID &"&FromAttach=Yes&FileSizeTooBig=Yes")
          Else
               FileExtension=ucase(mid(file.filename,instrRev(file.Filename,".")))
               Response.Write "FileExtension: " & FileExtension & "<br>"
               Select Case FileExtension
                    Case ".PDF",".DOC",".XLS",".PPT",".JPG","JPEG",".GIF",".BMP"
                         FileTypeOK=True
                    Case Else
                         FileTypeOK=False
               End Select
               if filetypeOK=False then
                  Response.Redirect("moc_attachments.asp?FromAttach=Yes&varMOCID="& varMOCID &"&FileTypeOK="& FileTypeOK &"&FileSizeTooBig=No&varIcon="& varIcon &"&FileExtension="& FileExtension &"")
            else
                  ' do save file
            end if
          End If
      Next
     End If
0
 
GaryCommented:
filetype=right(Uploader.Form("FILE1"),instrRev(Uploader.Form("FILE1"),".")-2)
0
 
GaryCommented:
Try that again
filetype=mid(Uploader.Form("FILE1"),instrRev(Uploader.Form("FILE1"),".")+1)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kevp75Commented:
you may have to do something like this:
Uploader.Form("FILE1").Value

I haven't used that uploader before, but I use another one, and it has that property built in
0
 
kevp75Commented:
or could it be...

Uploader("FILE1").Value   ???
0
 
sandip132Commented:
Try this:
MyUploader.Files("file1").FileName

0
 
okcuserAuthor Commented:
I tried Gary's solution and although it did not return an error, it also did not return the filename or the file extension.

Kev72's and Sandip132's solutions both returned "Object required" errors.

Does it matter that the form and the code are on two different pages? The form is located on a page called moc_attachments.asp and the code that I would like to return the form field name is on moc_attachments2.asp. Typically, I would use a request.form to return the value, but it is different with the uploader. I have another form field on the form called FileDesc and I can return the value of that field just fine, but it is text (type="text") as opposed to the FILE field, which is type="File".

This is the code that is located on the moc_attachments2.asp page:

     varMOCID=Request.QueryString("varMOCID")      
     Dim Uploader, File
     Set Uploader = New FileUploader
      
     Uploader.Upload()
      
     If Uploader.Files.Count = 0 Then
          Response.Redirect("moc_attachments.asp?varMOCID="& varMOCID &"&FromAttach=Yes&FileAdded=No")
     Else
          MAXSIZE = 2000000
          FileSize=Request.TotalBytes
            
          If FileSize > MAXSIZE Then
               Response.Redirect("moc_attachments.asp?varMOCID="& varMOCID &"&FromAttach=Yes&FileSizeTooBig=Yes")
          Else
               FileExtension=Uploader.Form("FILE1")
               Response.Write "FileExtension: " & FileExtension & "<br>"
                  
               Select Case FileExtension
                    Case ".PDF",".DOC",".XLS",".PPT",".JPG","JPEG",".GIF",".BMP"
                         FileTypeOK=True
                    Case Else
                         FileTypeOK=False
               End Select
               Response.Redirect("moc_attachments.asp?FromAttach=Yes&varMOCID="& varMOCID &"&FileTypeOK="& FileTypeOK &"&FileSizeTooBig=No&varIcon="& varIcon &"&FileExtension="& FileExtension &"")
          End If
     End If

Any other ideas why it would not return the value?

Thanks,

Tammi
0
 
okcuserAuthor Commented:
Sandip132's solution returned the following error:

     Microsoft VBScript runtime  error '800a01a8'

     Object required: 'Form(...)'

     /TonerSafety/moc_attachments2.asp, line 24

Kev75's solution returned the following error:

     Microsoft VBScript runtime  error '800a01a8'

     Object required: 'Form(...)'

     /TonerSafety/moc_attachments2.asp, line 24

Where Line 24 is (respectively):

     FileExtension=Uploader.Form("FILE1").FileName
     FileExtension=Uploader.Form("FILE1").Value

Thanks,

Tammi
0
 
GaryCommented:
What does FileExtension return at this point, response.write the value out

               FileExtension=Uploader.Form("FILE1")
               Response.Write "FileExtension: " & FileExtension & "<br>"
0
 
okcuserAuthor Commented:
Hi Gary,

It returns nothing. The response.write (Response.Write "FileExtension: " & FileExtension & "<br>") returns:

FileExtension:

Tammi
0
 
GaryCommented:
Looking at your logic it looks like you redirect to moc_attachments.asp no matter what.

     If Uploader.Files.Count = 0 Then
          ' no files so redirect to moc_attachments.asp
     Else
          MAXSIZE = 2000000
          FileSize=Request.TotalBytes
         
          If FileSize > MAXSIZE Then
               ' if file size too big then redirect to moc_attachments.asp
          Else
               '  anything else still redirect to moc_attachments.asp
          End If
     End If

Are you sure the page isnt being redirected on first load, may explain why theres no value in Uploader.Form("FILE1")
0
 
okcuserAuthor Commented:
Gary,

I don't think so, or it wouldn't write my response.write. The response.write is only on moc_attachments2.asp.

It just doesn't make sense, because if I replace FILE1 (the name of the file field on the form) with FileDesc (the name of the description text field on the form), then it returns the value of FileDesc. The only difference between the two is their type.

Thanks,

Tammi
0
 
okcuserAuthor Commented:
This is the contents of the upload.asp file:

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

      Public Property Get Form(sIndex)
            Form = ""
            If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
      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
0
 
GaryCommented:
Try it on my end and works fine
0
 
okcuserAuthor Commented:
ACK ACK!

     Microsoft VBScript runtime  error '800a01a8'

     Object required: 'Uploader'

     /TonerSafety/moc_attachments2.asp, line 7

0
 
kevp75Commented:
I think youwill have to go with a different uploader.  Looking through the Class that you posted, it doesn't look like it supports form fields....
0
 
GaryCommented:
Did you include the rest of the page i.e.
    varMOCID=Request.QueryString("varMOCID")    
     Dim Uploader, File
     Set Uploader = New FileUploader
     
     Uploader.Upload()

etc?
0
 
okcuserAuthor Commented:
Whoops! No, but I figured it out right quick AND NOW IT WORKS!!!!

Thanks so much!

Tammi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.