Solved

File upload / Binaryread problem

Posted on 2004-09-14
1
326 Views
Last Modified: 2008-02-01
I have a file system in place where users can browse a list of directories using an interface and upload or download files accordingly.

The file upload uses an object not written by myself. The object uploads the file to the required directory but with no database record insert.

I need to add the ability to upload the file to the right directory and also to insert a record in the database for it with some extra information such as a description. I have so far got it to insrt the name of the file and the full path but I need it to insert the description as well from the 'Meta' field without it giving me errors relating to binaryread and form requests. Any help greatly appreciated. :o)

The form has 2 fields "file1" and "Meta" and the form targets itself.

Below is the code for the upload and also the object code for the upload object.

----------------------------------------------------------------------------------------------------------------
upload.asp (form page)
----------------------------------------------------------------------------------------------------------------

<!--#include file ="config.asp"-->
<!--#include file ="upload_fso.asp"-->
<%
Server.ScriptTimeout = 7000

Set oUpload = New Upload
'oUpload.AllowedTypes = "gif|jpg"
oUpload.AllowedTypes = "*" 'Accept all file types
oUpload.MaxFileSize = 30000000

If oUpload.IsUpload Then
      oUpload.Recieve()

      If oUpload.ErrNum=0 Then
            oUpload.SaveFile sBaseFolder & oUpload.Value("inpCurrFolder") & "\" & oUpload.Value("File1")
            sMsg = "File saved."
      Else
            'Eg.
            'ErrNum=1: The posted data exceeds the maximum size allowed.
            'ErrNum=2: The File Type is not allowed.
            sMsg = oUpload.ErrMsg
      End If
      
      sHTML = "<html><head><title>Upload</title><style>" & _
                  "body {font:8pt verdana,arial,sans-serif}" & VbCrLf & _
                  "input {font:8pt verdana,arial,sans-serif}" & VbCrLf & _
                  "td {font-size:10}" & VbCrLf & _
                  "</style>" & VbCrLf & _
                  "<body onUnLoad=""dialogArguments.txtNeedRefresh.value='yes'"" style=""overflow:hidden;margin:0;"">" & VbCrLf & _
                  "<table width=100% height=100% ><tr><td valign=middle align=center>" & VbCrLf & _
                  sMsg & "<br><br><INPUT type=button value=' ok ' onclick='self.close()'>" & VbCrLf & _
                  "</td></tr><table>" & VbCrLf & _
                  "</body></html>"
      
      Response.Write sHTML
      response.End
End If
Set oUpload = Nothing
%>

----------------------------------------------------------------------------------------------------------------
upload_fso.asp
----------------------------------------------------------------------------------------------------------------
<%
Class Upload

      Private bFile
      Private dictRequest
      
      Private dictRequestFiles0
      Private dictRequestFiles1
      Private dictRequestFiles2
      Private dictRequestFiles3
      Private dictRequestFiles4
      Private dictRequestFiles5
      
      Private sAllowedTypes
      Private nMaxFileSize
      Private nErrNum
      Private sErrMsg
      
      Public Function IsUpload()
            sContentType = Request.ServerVariables("HTTP_CONTENT_TYPE")
            if InStr(sContentType,"multipart/form-data")=0 Then      
                  IsUpload = false
            else
                  IsUpload = true
            End If
      End Function
      
      Public Sub Recieve()
      
            nErrNum = 0
            
            sContentType = Request.ServerVariables("HTTP_CONTENT_TYPE")      
                  
            binData = Request.BinaryRead(Request.TotalBytes) '1
            if Request.Totalbytes > nMaxFileSize then
                  nErrNum = 1
                  sErrMsg = "The posted data exceeds the maximum size allowed."
            End If      
            'binData = Request.BinaryRead(Request.TotalBytes) '2
            
            'Kalau 1 enable => tdk error
            'Kalau 1 disable, 2 enable => error            
            
            lenBinData = lenB(binData)
            set adoRs = server.CreateObject("ADODB.Recordset")
            If lenBinData>0 Then
                  adoRs.Fields.Append "UploadData", 201, lenBinData
                  adoRs.Open
                  adoRs.AddNew
                  adoRs("UploadData").AppendChunk binData
                  adoRs.Update
                  sData = adoRs("UploadData")'Char
            End If      
            
            arrTemp = split(sContentType,";")
            sBoundary = Split(Trim(arrTemp(1)), "=")(1)
            arrFieldValue = Split(sData,sBoundary)
                        
            set dictRequest = server.CreateObject("Scripting.Dictionary")
            sBrowser = UCase(Request.ServerVariables("HTTP_USER_AGENT"))
                        
            bFile=false
            For i=0 To UBound(arrFieldValue)
                  fieldSeparate = InStr(arrFieldValue(i), Chr(13) & Chr(10) & Chr(13) & Chr(10))
                  If fieldSeparate>0 Then
                        fieldEnd      = fieldSeparate-3
                        valueStart      = fieldSeparate+4
                        valueEnd      = Len(arrFieldValue(i)) - fieldSeparate - 4 - 3
                                    
                        sFieldRaw      = Mid(arrFieldValue(i), 3 , fieldEnd)
                        sValue            = Mid(arrFieldValue(i), valueStart , valueEnd)

                        If InStr(sFieldRaw,"filename=")>0 Then

                              sLocal = getLocal(sFieldRaw)
                              If InStr(sBrowser,"WIN")>0 Then
                                    posStart = InStrRev(sLocal, "\") + 1
                                    sFileName = Mid(sLocal, posStart)
                              End If                              
                              If InStr(sBrowser,"MAC")>0 Then
                                    sFileName = sLocal
                              End If                              
                                          
                              dictRequestFiles0 = getFieldName(sFieldRaw)
                              dictRequestFiles1 = sFileName
                              dictRequestFiles2 = sValue
                              dictRequestFiles3 = sLocal
                              dictRequestFiles4 = getFileType(sFieldRaw)

' Start insert Record
strCon = "Driver={SQL Server};Server=192.168.0.6;Database=FileManager;"
Set rsAddFile = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM dbo.Files"
rsAddFile.Open strSQL, strCon, 3, 3
rsAddFile.AddNew
rsAddFile.Fields("FileName") = oUpload.Value("File1")
rsAddFile.Fields("Meta") = Request.Form("Meta")
rsAddFile.Fields("Directory") = Request.ServerVariables("URL")
rsAddFile.Update
rsAddFile.Requery
rsAddFile.Close
Set rsAddFile = Nothing
' End insert record                              
                              
                              if Not IsAllowed(sFileName) then
                                    nErrNum = 2
                                    sErrMsg = "The File Type is not allowed."
                              end if
                              
                              bFile=true
                        Else
                              dictRequest.Add getFieldName(sFieldRaw),sValue            
                        End If
                  End If
            Next
      End Sub

      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      '      PRIVATE
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      Private Function getFieldName(s)
            posStart = InStr(s, "name=") + 6 '6 krn ada tambahan "
            if InStr(s,Chr(34) & ";")>0 Then 'Chr(34) = "            
                  's => Content-Disposition: form-data; name="File1"; filename="C:\Documents and Settings\Ys\My Documents\mytext.txt"
                  '        Content-Type: text/plain
                  posEnd = InStr( posStart , s, Chr(34) & ";" )
            Else
                  's => Content-Disposition: form-data; name="inpNewFileName"
                  posEnd = inStr( posStart , s, Chr(34))            
            End If      
            getFieldName = Mid(s, posStart , posEnd - posStart)
      End Function

      Private Function getLocal(s)
            posStart = InStr(s, "filename=") + 10
            posEnd = InStr(s, Chr(34) & Chr(13) & Chr(10))
            getLocal = Mid(s, posStart, posEnd-posStart)
      End Function

      Private Function getFileType(s)
            posStart = InStr(s, "Content-Type: ")
            GetFileType = Mid(s, posStart + 14)
      End Function
      
      Private Function IsAllowed(sFileName)
            For Each Item In Split(sFileName,".")
                  sExtention = Item
            Next

            IsAllowed = false
            For Each Item In Split(sAllowedTypes,"|")
                  If LCase(Item) = LCase(sExtention) or LCase(Item) = "*"  Then
                        IsAllowed = true
                  End If
            Next
      End Function      
      
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      '      PUBLIC
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      Public Property Let AllowedTypes(sVal)
            sAllowedTypes = sVal
      End Property

      Public Property Let MaxFileSize(nVal)
            nMaxFileSize = nVal
      End Property

      Public Property Get ErrNum
            ErrNum = nErrNum
      End Property
      
      Public Property Get ErrMsg
            ErrMsg = sErrMsg
      End Property            
      '~~~~~~~~~~~~      
      
      Public Function Value(s)
            if bFile=true then
                  if dictRequestFiles0 = s Then
                        Value = dictRequestFiles1
                  else
                        Value = dictRequest(s)
                  end if
            else
                  Value = dictRequest(s)
            end if
      End Function
      
      Public Function SaveFile(sPath)
            Set fso = server.CreateObject("Scripting.FileSystemObject")
            Set sFile = fso.CreateTextFile(sPath, True)
            sFile.Write(dictRequestFiles2)
            sFile.Close      
            Set fso = Nothing
      End Function
      
End Class
%>
0
Comment
Question by:missionad
1 Comment
 
LVL 15

Accepted Solution

by:
OMC2000 earned 500 total points
ID: 12056249
You should not insert record inside the code.
The good practice is to let control process request, then get values of form fields from control and insert values.

upload.asp:

If oUpload.IsUpload Then
     oUpload.Recieve()
' Start insert Record
strCon = "Driver={SQL Server};Server=192.168.0.6;Database=FileManager;"
Set rsAddFile = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM dbo.Files"
rsAddFile.Open strSQL, strCon, 3, 3
rsAddFile.AddNew
rsAddFile.Fields("FileName") = oUpload.Value("File1")
rsAddFile.Fields("Meta") = oUpload.Value("Meta") 'like this
rsAddFile.Fields("Directory") = Request.ServerVariables("URL")
rsAddFile.Update
rsAddFile.Requery
rsAddFile.Close
Set rsAddFile = Nothing
' End insert record
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamic? Dropdown Box 29 70
How do i Properly Convert Date time String in C# 29 59
Out the count variables 6 48
Issue with Loop 4 42
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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

17 Experts available now in Live!

Get 1:1 Help Now