HTTP File Upload without User Interaction using .NET

This article aims to teach users how to upload files by using .NET without getting messy, as can happen with HTTP. The author looks at possible solutions and explains why they do not work, then goes on to outline his solution.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 33
October 26, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Introduction

This article addresses the problem of uploading a file without user interaction, similar to an article I previously wrote, "HTTP File Download without user interaction using .NET". Most of the upload code available depends on the user to browse his or her hard disk to select the file (based on RFC1867), and it doesn’t display a progress bar while uploading it to the server. This article provides an appropriate solution for it.

Concept

The idea is to have an ActiveX control (for other alternatives, read the download article noted above) read the file from the client's hard disk and write it into the Request stream of HTTP. The ASP.NET code on the server will read the file content from the Request stream and write it into the hard disk.

About ActiveX Control

The term 'ActiveX' refers to an extension of existing OLE and COM technologies. An ActiveX control is an object that supports a customizable, programmatic interface. Using the methods, events, and properties exposed by a control, Web authors can automate their HTML pages. Examples of such controls include text boxes, command buttons, audio players, video players, stock tickers, and so on.

The following are the possible solutions which can be used to solve our problem.

  1. INET control - This is an ActiveX control provided by Microsoft for Internet transfer of files. We can read the file contents into a string and send it as POST data using the function Execute provided by the control. If all you want to do is upload the file, then this method should suffice, but if you want to display the progress as well, then this solution can't be used as it doesn’t provide the number of bytes being uploaded.

  2. Wininet dll - This DLL contains a set of APIs provided by Microsoft for Internet transfer of files. Performance is better in this case since we are directly using the APIs and not relying on another control to provide the functionality. This is the DLL which I used for downloading the file but sadly for uploading the file, it has a bug (it always returns 0 when it should return 1) in its InternetWriteFile function.

  3. Winsock control - This is an ActiveX control provided by Microsoft for network programming. It uses sockets to connect to the server and get the files. It’s a low level control where the programmer has to do the bulk of the work, but luckily it provides sendProgress event which gives information about the number of bytes uploaded. So we would be using this control to solve our problem.

Solution

Software requirements -

  1. VB6 for client side code
  2. .NET framework for server side code
  3. Windows 2000 or NT and above

Server side code

  1. Create a new ASP.NET project with VB.NET as code behind and name it as MyUpload.

  2. 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.

  3. Build the project and it's all set as far as the server side code is concerned.

Client Side Code

  1. Create a new ActiveX control VB project called FileUpload.

  2. Change the name of the user control to FileUploadCtrl.

  3. Add a frame to the user control and place the following labels, progress bar inside the frame and WinSock Control.
    1. lblByteU - set its caption to "Bytes Uploaded".
    2. lblBytesUploaded - set its caption to "".
    3. lblFileS - set its caption to "FileSize in Bytes".
    4. lblFileSize - set its caption to "".
    5. pgUpload - set progressbar name to "pgUpload" 
    6. wsUpload - set WinSock control name to "wsUpload"

      Http File Upload

  4. Use the "ActiveX control interface wizard" in VB6's add-in menu to create functions with following declaration:
    1. 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.

    2. 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

    3. 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

  5. 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

Explanation of Code for Upload Function

Let me break up the code for Upload function so that it’s easier to understand.

  • First let’s declare the following variables which we would need inorder to implement the upload functionality.

On Error GoTo errhandle
Dim lngFileSize As Long
Dim strData As String
Dim strPostData As String

  • Read the size of the file to be uploaded and initialize the labels and progress bar.

'read file length
lngFileSize = FileLen(strFileName)
lngFileLen = lngFileSize
pgUpload.Value = 0
lblFileSize.Caption = lngFileLen

  • Next step is to read the file into a string.

Open strFileName For Binary As #2
        strData = Input(FileLen(strFileName), #2)
  Close #2

  • Construct the HTTP Request packet, by first creating the header of the packet giving the protocol being used for sending the request. We will be using POST to send data due to its support of large volume of data. We also specify the content type of the data and what’s the length of the data

strPostData = "POST " & strURL & vbCrLf
    strPostData = strPostData & "Content-Type: application/octet-stream" & vbCrLf
    strPostData = strPostData & "Content-Length: " & Format$(Len(strData)) & vbCrLf
    strPostData = strPostData & "Connection: Keep-Alive" & vbCrLf & vbCrLf

  • Complete the HTTP Request packet by appending the data payload.

strPostData = strPostData & strData & vbCrLf

  • Now all we need to do is send the Post Data through the Winsock control and complete error handling section

wsUpload.SendData strPostData
       
UploadFile = True

Exit Function
errhandle:
    nErrNo = Err.Number
    UploadFile = False

  • Copy the following lines of code to implement the progress bar functionality.

'This event is fired once the data has been sent completely
Private Sub wsUpload_SendComplete()
lngTotalBytesSent = 0
lblBytesUploaded.Caption = lngFileLen
pgUpload.Value = 100
End Sub

'This event is fired once the sendData function is called
Private Sub wsUpload_SendProgress(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
Dim nPercent As Integer
lngTotalBytesSent = lngTotalBytesSent + bytesSent
nPercent = Int((lngTotalBytesSent / lngFileLen) * 100)
pgUpload.Value = nPercent
lblBytesUploaded.Caption = lngTotalBytesSent
lblBytesUploaded.Refresh
pgUpload.Refresh
End Sub
  

   7. With the above steps, we have finished the code for uploading the files. Now only the creation of the activex control remains. To create it, click on File menu of VB IDE and there will be a selection called "make fileupload.ocx", click on it and Voila, your activex control is ready.

CAB File

   8. In order for the control to be downloaded over the Internet, we need to package it into CAB file. This can be done with the help of "Package and Deployment Wizard" provided Visual Studio6.

   9. The wizard would generate a CAB file and htm file. Copy these 2 files to your webserver's virtual directory and write a small javascript function in the htm file which would call the control's uploadFile method. Eg

<script language=javascript>
function upload()
{
 var b,errno,errdesc,errsrc;
FileUploadCtrl.uploadFile("
http://localhost/Myupload/upload.aspx", "c:\\uploads\\uploadFile.doc",errno);
}
</script>

   10. The one thing that you have to keep in mind is you need to sign the activex control for it to be installed on the client machine without changing the security settings of Internet Explorer.

If you are planning to give an uninstalled activex control then on the client machine' Internet Explorer, you've got to go to internet options->security->internet and reduce it to the lowest level.

Conclusion

In this article, we have seen how to write our own custom code to upload a file over the Internet. And I hope I have answered the questions of many people who have queried me on how to write the upload control.

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials