.NET
  Home arrow .NET arrow Page 2 - HTTP File Upload without User Interaction ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

HTTP File Upload without User Interaction using .NET
By: Chandru Prashanth
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2004-10-26

    Table of Contents:
  • HTTP File Upload without User Interaction using .NET
  • Solution
  • Explanation of Code for Upload Function
  • CAB File

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    HTTP File Upload without User Interaction using .NET - Solution


    (Page 2 of 4 )

    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

    More .NET Articles
    More By Chandru Prashanth


       · Interesting approach. There are two questionsa) Why not use a stream, so you don't...
       · Yes, it is not as simple as the author has presented here in this ariticle...The...
       · I cant put my finger on a specific problem, however getting INET or winsock controls...
       · DITTO ALL THE WAY!
       · I'm trying to convert this code to C# but I'm having some trouble...For one I'm...
       · Hi Mr. Chandru,I tried using this code, although I used a third party winsock...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT