HTTP File Upload without User Interaction using .NET - Solution (Page 2 of 4 ) Software requirements - - VB6 for client side code
- .NET framework for server side code
- Windows 2000 or NT and above
Server side code - Create a new ASP.NET project with VB.NET as code behind and name it as MyUpload.
- Add a new webform called upload.aspx and paste this piece of code in the aspx file page_load procedure.
Private Sub Page_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim buff() as byte Dim fw As System.IO.FileStream = New System.IO.FileStream("c:\\upload.doc", IO.FileMode.Create) buff = Request.BinaryRead(Request.TotalBytes) fw.Write(buff, 0, buff.Length) fw.Close() End Sub
The above code reads the file content into a byte array called buff and then writes buff into a file using FileStream. It's very important and a good programming practice to close the file stream after using it.
- Build the project and it's all set as far as the server side code is concerned.
Client Side Code - Create a new ActiveX control VB project called FileUpload.
- Change the name of the user control to FileUploadCtrl.
- Add a frame to the user control and place the following labels, progress bar inside the frame and WinSock Control.
- lblByteU - set its caption to "Bytes Uploaded".
- lblBytesUploaded - set its caption to "".
- lblFileS - set its caption to "FileSize in Bytes".
- lblFileSize - set its caption to "".
- pgUpload - set progressbar name to "pgUpload"
- wsUpload - set WinSock control name to "wsUpload"

- Use the "ActiveX control interface wizard" in VB6's add-in menu to create functions with following declaration:
- Public Function Connect(strServerName As String, ByRef errno As integer) As Boolean
Parameters - strServerName- Name of the server to connect to eg localhost.
- errno- Gives the errno if any error has occurred.
Returns - True or False depending on whether the operation was success or not.
- Public Function UploadFile(url As String, filename As String, ByRef errno As integer) As Boolean
Parameters - url - is the url of your upload.aspx file e.g. http://localhost/MyUpload/upload.aspx
- filename- is the name of the file to upload
- errno- gives the errorno if any error has occurred
Returns - True or false depending on whether the operation was success or not
- Public Function Disconnect(ByRef errno As Integer) As Boolean
Parameters - errno- gives the errorno if any error has occurred
Returns - True or false depending on whether the operation was success or not
- Once you have completed the wizard, Locate Connect function in the code and add these statements.
On Error GoTo errhandle wsUpload.RemoteHost = strServerName wsUpload.RemotePort = 80 wsUpload.Connect lblconn.Caption = "not connected" While wsUpload.State <> sckConnected DoEvents Wend lblconn.Caption = "connected" Connect = True Exit Function errhandle: nErrNo = Err.Number Connect = False
What the code above does, is to open a socket connection to the server mentioned in strServerName through port 80 (HTTP server listens on this port).
For Disconnect function, paste the following line of the code, to close the socket connection:
wsUpload.Close
Next: Explanation of Code for Upload Function >>
More .NET Articles More By Chandru Prashanth |