binary file upload conversion from VB6 Inet to VB.NET ??

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the last and most difficult part of my first application conversion from VB6 to VB.NET.
The enclosed code took months even though I had help along the way.
The purpose of the code is to upload a binary file. In VB6 it used the Inet method.
The CmdUpload_Click code works - its just the upload interface in VB.NET I havent figured out.
I successfully implemented WebRequest for downloads and it works great.
I then studied HttpWebRequest thinking that was what I needed for my upload.
Unfortunately Im stuck - maybe because Im trying to find similarities with Inet?

Thanks for this and all the wonderful help so far - ftbrady


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Private <span style="color:Blue; Sub CmdUpload_Click()

<span style="color:Blue; Dim Params <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim Headers <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim Boundary <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim FileData <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim filename <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim pathname <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim LotWUploadURL <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Dim re <span style="color:Blue; As <span style="color:Blue; Short

<span style="color:Green; set output (download response) file name
OutputFileName = <span style="color:#A31515; "c:hamlogupldreport.txt"

<span style="color:Green; erase contents of output file
re = FreeFile()
FileOpen(re, OutputFileName, OpenMode.Output)
FileClose((re))

Boundary = <span style="color:#A31515; "01014032"

filename = <span style="color:#A31515; "W0ECS__ 9__06Oct2006__.tq8"

pathname = <span style="color:#A31515; "c:hamlogadif_files" & filename

<span style="color:Green; read tq8 file contents into FileData string
FileData = GetFileContents(pathname)


<span style="color:Blue; While InStr(FileData, Boundary) > 0
System.Windows.Forms.Application.DoEvents()
Boundary = VB6.Format(Val(Boundary) + 1)
<span style="color:Blue; End <span style="color:Blue; While

<span style="color:Green; >>>>> Inet1.Cancel() Stops any current operations
<span style="color:Green; >>>>> Inet1.Protocol = InetCtlsObjects.ProtocolConstants.icHTTPS
<span style="color:Green; >>>>>>>>>>>>> This is the 1st place Im stuck

LotWUploadURL = <span style="color:#A31515; "HTTPS://p1k.arrl.org/lotw/upload"

Params = <span style="color:#A31515; "--" & Boundary & vbCrLf

Params = Params & <span style="color:#A31515; "Content-Disposition: form-data; name=""upfile""; filename=""" & filename & <span style="color:#A31515; """" & vbCrLf

Params = Params & <span style="color:#A31515; "Content-Type: text/plain" & vbCrLf & vbCrLf

Params = Params & FileData & vbCrLf

Params = Params & <span style="color:#A31515; "--" & Boundary & vbCrLf

Headers = <span style="color:#A31515; "Content-Type: multipart/form-data; boundary=" & Boundary & vbCrLf

<span style="color:Green; >>>>> Inet1.Execute(LotWUploadURL, "POST", Params, Headers)
<span style="color:Green; >>>>>>>>>>>>> This is the 2nd place Im stuck

<span style="color:Blue; End <span style="color:Blue; Sub





<span style="color:Blue; Private <span style="color:Blue; Sub Inet1_StateChanged(<span style="color:Blue; ByVal state <span style="color:Blue; As <span style="color:Blue; Integer)

<span style="color:Blue; Dim vtData <span style="color:Blue; As <span style="color:Blue; Variant
<span style="color:Blue; Dim re <span style="color:Blue; As <span style="color:Blue; Integer

<span style="color:Blue; On <span style="color:Blue; Error <span style="color:Blue; GoTo db

<span style="color:Blue; Select <span style="color:Blue; Case state

<span style="color:Blue; Case icResolvingHost <span style="color:Green; 1 The control is looking up the IP address of the specified host computer.
<span style="color:Blue; Case icHostResolved <span style="color:Green; 2 The control successfully found the IP address of the specified host computer.
<span style="color:Blue; Case icConnecting <span style="color:Green; 3 The control is connecting to the host computer.
<span style="color:Blue; Case icConnected <span style="color:Green; 4 The control successfully connected to the host computer.
<span style="color:Blue; Case icRequesting <span style="color:Green; 5 The control is sending a request to the host computer.
<span style="color:Blue; Case icRequestSent <span style="color:Green; 6 The control successfully sent the request.
<span style="color:Blue; Case icReceivingResponse <span style="color:Green; 7 The control is receiving a response from the host computer.
<span style="color:Blue; Case icResponseReceived <span style="color:Green; 8 The control successfully received a response from the host computer.
<span style="color:Blue; Case icDisconnecting <span style="color:Green; 9 The control is disconnecting from the host computer.
<span style="color:Blue; Case icDisconnected <span style="color:Green; 10 The control successfully disconnected from the host computer.
<span style="color:Blue; Case icError <span style="color:Green; 11 An error occurred in communicating with the host computer
Inet1.Cancel

<span style="color:Blue; Case icResponseCompleted <span style="color:Green; 12 The request has completed and all data has been received
<span style="color:Green; Loop: get chunks of the response
<span style="color:Blue; Do
DoEvents
vtData = Inet1.GetChunk(1024, icString)
strdata = strdata & vtData
<span style="color:Blue; Loop <span style="color:Blue; Until Len(vtData) = 0

<span style="color:Green; Save the response to "Return.htm" in the default directory
re = FreeFile
Open AppPath & <span style="color:#A31515; "Return.htm" <span style="color:Blue; For Output <span style="color:Blue; As #re
Print #re, strdata
Close (re)

<span style="color:Green; Save the response in user specified file format if applicable
<span style="color:Blue; If Len(OutputFileName) <> 0 <span style="color:Blue; Then
re = FreeFile
Open OutputFileName <span style="color:Blue; For Output <span style="color:Blue; As #re
Print #re, strdata
Close (re)
<span style="color:Blue; End <span style="color:Blue; If

<span style="color:Green; tell timer process that response download is complete
Return_Status = -1

<span style="color:Blue; Case <span style="color:Blue; Else
Log <span style="color:#A31515; "Unexpected Inet1_StateChanged state (state = " + str(state) + <span style="color:#A31515; ")"

<span style="color:Blue; End <span style="color:Blue; Select

<span style="color:Blue; Exit <span style="color:Blue; Sub

db:
Log <span style="color:#A31515; "Inet1_StateChanged error trap " + Err.Description + <span style="color:#A31515; " in state " + str(state)
Err.Clear
<span style="color:Blue; End <span style="color:Blue; Sub

[/code]
<br/>
<br/>

<hr class="sig ftbrady

View the full article
 
Back
Top