Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File upload / Binaryread problem

Posted on 2004-09-14
1
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
OMC2000 earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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