Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Windows Forms » Upload any type of file through a Visual Basic 2005 Web Service

Upload any type of file through a Visual Basic 2005 Web Service


This article shall describe an approach that may be used to upload any sort of a file through a web service from a Windows Forms application. The approach demonstrated does not rely on the ASP.NET file uploader control and allows the developer the opportunity to upload files programmatically and without user intervention.

Author Rank:
Total page views :  20552
Total downloads :  648
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
UploadAnyFileVBpack.zip
 
Become a Sponsor

Introduction

This article shall describe an approach that may be used to upload any sort of a file through a web service from a Windows Forms application. The approach demonstrated does not rely on the ASP.NET file uploader control and allows the developer the opportunity to upload files programmatically and without user intervention. Such an approach may be useful for doing something like processing out the contents of a local message queue when internet service is available (if the user base were mobile and had only intermittent connectivity). The article also addresses the use of a file size check as a precursor to allowing a file to upload through the service.



Figure 1: Test Application Shown Uploading a File.



Figure 2: Mixed bag of different file types in transient storage folder.

Getting Started

The solution contains two projects; one is a ASP.NET Web Service project (Uploader) and the other is a Win Forms test application (TestUploader) used to demonstrate uploading files through the web method provided in the web service project.

The web service project contains only a single web service (FileUploader) which in turn contains only a single Web Method (UploadFile). The Win Forms application contains only a single form which contains the controls (one textbox and two buttons used in conjunction with an OpenFileDialog control) and code necessary to select and upload files through the web service.



Figure 3:Solution Explorer with the both Projects Visible.

Code: Uploader Web Service Project

The Uploader web service project is an ASP.NET web service project containing a single web service called, "FileUploader"; this web service exposes a single web method called, "UploadFile".

The code for this web service begins with the following:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO

''' <summary>
''' This web method will provide an web method to load any
''' file onto the server; the UploadFile web method
''' will accept the report and store it in the local file system.
'''
</summary>
''' <remarks></remarks>

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class FileUploader
     Inherits System.Web.Services.WebService

The class starts out with the default imports; I added System.IO to the defaults to support the use of file and memory streams.  The web service namespace is left as the default http://tempuri.org/ which of course will have to updated if the service were deployed.

The remainder of the code supplied in this class is used to define the web method used to upload the file; the code is annotated.  The essential process is that, files converted to byte arrays are passed along with the full name of the file (not the path) including the extention as arguments to the UploadFile web method. The byte array is passed to a memory stream, and a file stream is opened pointing to a newly created file (named the name of the original file) within the target folder used to store the files. Once the file stream has been created, the memory stream is written into the file stream and then the memory stream and file stream are disposed of. 

The web method is setup to return a string; if all goes well, the string returned will read, "OK", if not, the error message encountered will be returned to the caller.

<WebMethod()> _

Public Function UploadFile(ByVal f As Byte(), ByVal fileName As String) As String

    ' the byte array argument containst the content of the file
    ' the string argument contains the name and extension of the 
    ' file pass in through the byte array

    Try

        ' instance a memory stream and pass the byte array
        ' to its constructor
        Dim ms As New MemoryStream(f)

        ' instance a filestream pointing to the
        ' storatge folder, use the original file name
        ' to name the resulting file
        Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") & fileName, FileMode.Create)
        ms.WriteTo(fs)

        ' clean up
        ms.Close()
        fs.Close()
        fs.Dispose()

        ' return OK if we made it to here
        Return "OK"

    Catch ex As Exception

        Return ex.Message.ToString()

    End Try

End Function

Code:  Test Uploader Win Forms Application

The test application contains a single Windows Form class; this form contains a text box used to display the name of the file selected for upload, a browse button used to launch an open file dialog box which is used to navigate to and select a file for upload, and an upload button which is used to pass the file to web service so that the selected file may be stored on the server.

The code for this class begins with the following:

Imports System.IO
    Public Class Form1

Aside from the default imports, I have added only System.IO to the list. This being necessary to support working with files. The namespace and class declarations are in the default configuration. In addition to System.IO, the project also adds in a web reference pointing to the File Uploader web service, the reference is given the alias of Uploader.

The next bit of code in the class is private method used to prepare the file for submittal to the web service and to actually make that submittal. The code below is annotated to describe the activity but the essential parts of the operation are to check the file size to see if the web service will accept the file (by default, the web server will accept uploads smaller than 4 MB in size, the web config file must be updated in order to support larger uploads), and to convert the file to a byte array. When everything is ready, the byte array and the name of the file including the extension is passed to an instance of the web service web method. 

Note that, when setting up the demo, you will have remove and add the web reference back into the project in order for it to work for you.

Public Sub UploadFile(ByVal filename As String)

    Try

        ' get the file name from the path
        Dim strFile As String = System.IO.Path.GetFileName(filename)

        ' create an instance of the web service
        Dim svc As New TestUploader.Uploader.FileUploader()

        ' get the file information for the selected file
        Dim fInfo As New FileInfo(filename)

        ' get the length of the file to see if it is possible
        ' to upload it (with the standard 4096 kb limit)
        Dim numBytes As Long = fInfo.Length
        Dim dLen As Double = Convert.ToDouble(fInfo.Length / 1000000)

        ' look for an overrun on file size
        If (dLen < 5) Then

            ' set up a filestream and binary reader for the file
            Dim fStream As New FileStream(filename, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fStream)

            ' convert the file to a byte array
            Dim data As Byte() = br.ReadBytes(Convert.ToInt32(numBytes))
            br.Close()

            ' pass the byte array and file name to the web method
            Dim sTmp As String = svc.UploadFile(data, strFile)
            fStream.Close()
            fStream.Dispose()

            ' this is always OK unless an error occurs
            MessageBox.Show("File upload status: " & sTmp, "File Upload")

        End If

    Catch ex As Exception

        MessageBox.Show(ex.Message.ToString(), "Upload Error")

    End Try

End Sub

Following the UploadFile method, the next bit of code is used to handle the browse button's click event. This code is used merely to display an open file dialog to the user and to take the file selected through that dialog and display the file name in the form's file name text box.

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles btnBrowse.Click

    OpenFileDialog1.Title = "Open File"
    OpenFileDialog1.FileName = String.Empty

    Try 

        OpenFileDialog1.InitialDirectory = "C:\Temp"

    Catch ex As Exception 

        MessageBox.Show(ex.Message.ToString(), "Error") 

    End Try

    OpenFileDialog1.ShowDialog()

    If OpenFileDialog1.FileName = String.Empty Then
        Return
    Else
 

        txtFileName.Text = OpenFileDialog1.FileName 

    End If

End Sub

The class wraps up with the button click event handler for the Upload button. This handler merely checks for text in the file name text box and, if something is there, it sends the value to the Upload method.

Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles btnUpload.Click

    If txtFileName.Text <> String.Empty Then 

        UploadFile(txtFileName.Text) 

    Else 

        MessageBox.Show("You must select a file first.", "No File Selected") 

    End If

End Sub

That wraps up all of the client and server side code necessary to upload any sort of file to a server from a Win Forms application.

Summary

This article was intended to demonstrate an easy approach to uploading any sort of a file to a web server from a Win Forms application. This example uses the default upload size of 4096 KB, if you need to upload larger files, you will need to alter this value by changing the httpRuntime maxRequestLength property to the desired value; at the same time you may need to increase the executionTimeout property to a greater value as well in order to support longer upload times. Take care when altering the values as Microsoft has established the default 4 MB limit to provide some safety against attempts to upload extremely large files that may hamper access to the server.

From the web.config file, increasing the upload file size is accomplished in this section of the file:

<configuration>
    <
appSettings/>
    <
connectionStrings/>
    <
system.web>
        <!--

            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.

            Visual Basic options:
            Set strict="true" to disallow all data type conversions
            where data loss can occur.
            Set explicit="true" to force declaration of all variables.
       
-->     

      <httpRuntime
        executionTimeout="110"
        maxRequestLength="4096"
      />

 


Login to add your contents and source code to this article
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
ASP.Net 4 Hosting is here
Become a Sponsor
 Comments
urgent plz reply by SHREY On May 28, 2007
if i want to see the file what i have uploaded then what to do: my question can be understand like this as we upload a picture in orkut we can see what we have uploaded how canwe do that in our site plz reply me at shrey.scet@gmail.com
Reply | Email | Delete | Modify | 
viagra by watches On July 11, 2010
The word Viagra is synonymous to strong penile erection in the sexually enlightened world community. Business giants have already sensed the pulse of ED market and challenged Viagra online with equally efficient erectile dysfunction treatments. Not that the small fishes in the market are sitting idle...the market is flooded with many generic versions of branded ED pills along with those called Herbal Generic Cialis .
Reply | Email | Delete | Modify | 
ANTS Performance Profiler 6.0
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.