Link to home
Start Free TrialLog in
Avatar of Sabrin
Sabrin

asked on

upload file

hello,
I have a project that is using a module and I am wondering if its possible to remove
the module and make some changes so the app keeps working correctly without it!

this is the main.frm
Option Explicit
Dim blnConnected As Boolean

Private Function BuildFileUploadRequest(ByRef strData As String, ByRef DestUrl As URL, _
ByVal UploadName As String, ByVal FileName As String, ByVal MIMEType As String) As String
   
    Dim strHttp As String
    Dim strBoundary As String
    Dim strBody As String
    Dim lngLength As Long
       
    strBoundary = RandomAlphaNumString(32)

    strBody = "--" & strBoundary & vbCrLf
    strBody = strBody & "Content-Disposition: form-data; name=""" & _
                    UploadName & """; filename=""" & FileName & """" & vbCrLf
    strBody = strBody & "Content-Type: " & MIMEType & vbCrLf
    strBody = strBody & vbCrLf & strData
    strBody = strBody & vbCrLf & "--" & strBoundary & "--"
   
    lngLength = Len(strBody)
   
    strHttp = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
    strHttp = strHttp & "Host: " & DestUrl.Host & vbCrLf
    strHttp = strHttp & "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf
    strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
    strHttp = strHttp & strBody

    BuildFileUploadRequest = strHttp
End Function
                                     
Private Sub cmdUpload_Click()
    Dim strFile As String
    Dim strHttp As String
    Dim DestUrl As URL
    Dim MIMEType As String
    Dim Name As String
   
    If blnConnected Then Exit Sub
   
    DestUrl = ExtractUrl("http://mywebsite.com/upload/post.php")
   
    txtResponse.Text = ""
   
    strFile = GetFileContents(App.Path & "\mypicture.jpg")
   
    MIMEType = "application/octet-stream"
    Name = "file"
   
    strHttp = BuildFileUploadRequest(strFile, DestUrl, Name, "pictest.jpg", MIMEType)
   
    Winsock2.RemoteHost = DestUrl.Host
    Winsock2.Connect
   
    While Not blnConnected
        DoEvents
    Wend
   
    txtRequest.Text = strHttp
    Winsock2.SendData strHttp
End Sub

Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    Winsock2.GetData strData, vbString, bytesTotal
    txtResponse.Text = txtResponse.Text & strData
End Sub

Private Sub Winsock2_Connect()
    blnConnected = True
End Sub

Private Sub Winsock2_Close()
    Winsock2.Close
    blnConnected = False
End Sub

Private Function GetFileContents(ByVal strPath As String) As String
    Dim StrReturn As String
    Dim lngLength As Long
   
    lngLength = FileLen(strPath)
    StrReturn = String(lngLength, Chr(0))
   
    On Error GoTo ERR_HANDLER
   
    Open strPath For Binary As #1
   
    Get #1, , StrReturn
   
    GetFileContents = StrReturn
   
    Close #1
   
    Exit Function
   
ERR_HANDLER:
    MsgBox Err.Description, vbCritical, "ERROR"
   
    Err.Clear
End Function

Private Function RandomAlphaNumString(ByVal intLen As Integer)
    Dim StrReturn As String
   
    Dim X As Integer
    Dim c As Byte
   
    Randomize
   
    For X = 1 To intLen
        c = Int(Rnd() * 127)
   
        If (c >= Asc("0") And c <= Asc("9")) Or _
           (c >= Asc("A") And c <= Asc("Z")) Or _
           (c >= Asc("a") And c <= Asc("z")) Then
           
            StrReturn = StrReturn & Chr(c)
        Else
            X = X - 1
        End If
    Next X
   
    RandomAlphaNumString = StrReturn
End Function

----------------------------------------------------------------------------------------------------------------

'and this is the module
Option Explicit

Type URL
    Scheme As String
    Host As String
    Port As Long
    URI As String
    Query As String
End Type
   

' returns as type URL from a string
Function ExtractUrl(ByVal strUrl As String) As URL
    Dim intPos1 As Integer
    Dim intPos2 As Integer
   
    Dim retURL As URL
   
    '1 look for a scheme it ends with ://
    intPos1 = InStr(strUrl, "://")
   
    If intPos1 > 0 Then
        retURL.Scheme = Mid(strUrl, 1, intPos1 - 1)
        strUrl = Mid(strUrl, intPos1 + 3)
    End If
       
    '2 look for a port
    intPos1 = InStr(strUrl, ":")
    intPos2 = InStr(strUrl, "/")
   
    If intPos1 > 0 And intPos1 < intPos2 Then
        ' a port is specified
        retURL.Host = Mid(strUrl, 1, intPos1 - 1)
       
        If (IsNumeric(Mid(strUrl, intPos1 + 1, intPos2 - intPos1 - 1))) Then
                retURL.Port = CInt(Mid(strUrl, intPos1 + 1, intPos2 - intPos1 - 1))
        End If
    ElseIf intPos2 > 0 Then
        retURL.Host = Mid(strUrl, 1, intPos2 - 1)
    Else
        retURL.Host = strUrl
        retURL.URI = "/"
       
        ExtractUrl = retURL
        Exit Function
    End If
   
    strUrl = Mid(strUrl, intPos2)
   
    ' find a question mark ?
    intPos1 = InStr(strUrl, "?")
   
    If intPos1 > 0 Then
        retURL.URI = Mid(strUrl, 1, intPos1 - 1)
        retURL.Query = Mid(strUrl, intPos1 + 1)
    Else
        retURL.URI = strUrl
    End If
   
    ExtractUrl = retURL
End Function
Avatar of quiklearner
quiklearner

If the URL type isn't used in any other file in your project and the exacturl function isn't called from anywhere else you can easily move the entire contents of the module to the form..  I am not sure i really see any changes that will be necessary either
obviously lose the option explicit though
Avatar of Sabrin

ASKER

the reason why i dont want the module is because im trying to keep the code as little a possible
and puting the module in the form will give me the same amount of code!
then i will go through the code and do what i can.. give me a few..
Avatar of Sabrin

ASKER

thanks a lot!
I just noticed a problem where you are treating the contents of the file as a string..  I will correct this.  If this has worked before you got lucky..
ASKER CERTIFIED SOLUTION
Avatar of quiklearner
quiklearner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It took me probably a hlaf-hour or so to come up with the put() solution to how to combine the byte() with the string variable type..  I have done this differently before but that actually turned out to be awesome little trick i intend to use going forward!!