Solved

Uploader.Form (getting the name of the file)

Posted on 2006-10-26
18
206 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:okcuser
  • 7
  • 7
  • 3
  • +1
18 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 17815185
filetype=right(Uploader.Form("FILE1"),instrRev(Uploader.Form("FILE1"),".")-2)
0
 
LVL 58

Expert Comment

by:Gary
ID: 17815241
Try that again
filetype=mid(Uploader.Form("FILE1"),instrRev(Uploader.Form("FILE1"),".")+1)
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17815643
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
 
LVL 25

Expert Comment

by:kevp75
ID: 17815651
or could it be...

Uploader("FILE1").Value   ???
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17816963
Try this:
MyUploader.Files("file1").FileName

0
 

Author Comment

by:okcuser
ID: 17818857
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
 

Author Comment

by:okcuser
ID: 17818886
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
 
LVL 58

Expert Comment

by:Gary
ID: 17818946
What does FileExtension return at this point, response.write the value out

               FileExtension=Uploader.Form("FILE1")
               Response.Write "FileExtension: " & FileExtension & "<br>"
0
 

Author Comment

by:okcuser
ID: 17818989
Hi Gary,

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

FileExtension:

Tammi
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 58

Expert Comment

by:Gary
ID: 17819057
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
 

Author Comment

by:okcuser
ID: 17819085
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
 

Author Comment

by:okcuser
ID: 17819164
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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 17819190
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
 
LVL 58

Expert Comment

by:Gary
ID: 17819212
Try it on my end and works fine
0
 

Author Comment

by:okcuser
ID: 17819230
ACK ACK!

     Microsoft VBScript runtime  error '800a01a8'

     Object required: 'Uploader'

     /TonerSafety/moc_attachments2.asp, line 7

0
 
LVL 25

Expert Comment

by:kevp75
ID: 17819269
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
 
LVL 58

Expert Comment

by:Gary
ID: 17819270
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
 

Author Comment

by:okcuser
ID: 17819362
Whoops! No, but I figured it out right quick AND NOW IT WORKS!!!!

Thanks so much!

Tammi
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now