.NET
  Home arrow .NET arrow Page 3 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 / 22
    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 - Explanation of Code for Upload Function


    (Page 3 of 4 )

    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.

    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

    - 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
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





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