GOA - System.Net.FileUploader

FileUploader Class


Provides implementation for the upload of differents files.

Definition

public class FileUploader: Object, IDisposable

Members Table

MethodDescription
AbortCancels the upload of a file.
Dispose
EqualsOverloaded. Compares two objects to determine if they are equal. (Inherited from Object)
GetHashCodeServes as a hash function for a particular type, suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object)
GetTypeGets the Type of the current instance. (Inherited from Object)
OnStateChangedRaises the StateChanged event.
ReferenceEqualsDetermines whether the specified Object instances are the same instance. (Inherited from Object)
StartStarts the file upload.
ToStringReturns a String that represents the current Object. (Inherited from Object)
PropertyDescription
ContentLengthGets the total size of the file to upload.
CreationDateGets the creation date of the file to upload.
HttpStatusCodeGets a value indicating the status of the Http request related with the FileUploader instance.
ModificationDateGets the last modification date of the file to upload.
NameGets the name of the file to upload.
SpeedGets the speed of the uploading file.
StateGets the state of the FileUploader.
UploadedPercentageGets the uploaded percentage of the file.
UploadedSizeGets the size of the file already uploaded.
UrlGets the target of the file to upload.
EventDescription
StateChangedHandles the changes of state of the FileUploader instance.

Inheritance Hierarchy

Object
FileUploader

Examples

// This examples shows how to create and initialize a FileUploader and a FileUploaderDialog.

// It shows also some properties and method of these two classes.

 

/*

Uploader sample

This sample needs a target upload url.

Associated myupload.php code in this test: (put it on your server)

<?php

$file= $_FILES['Filedata'];

$name= $file['name']; // e.g. "image.jpg"

$file_mime_type= $file['type']; // application/octet-stream

$error= $file['error']; // 0 if none

$size= $file['size'];

$tmp_pathname= $file['tmp_name'];

mkdir( "/tmp/my_uploaded_files" );

move_uploaded_file( $tmp_pathname, "/tmp/my_uploaded_files/$name" );

?>

*/

 

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Diagnostics;

using System.Net;

 

namespace Uploader

{

     public class Form1 : Form

     {

          private TextBox url;

          private Button select;

          private Button start;

          private Button abort;

          private TextBox log;

 

          // Creates a FileUploader and the FileUploaderDialog associated with.

          private FileUploaderDialog dialog = null;

          private FileUploader uploader= null;

 

          public Form1()

          {

               InitializeComponent();

          }

 

          private void InitializeComponent()

          {

               url= new TextBox();

               select= new Button();

               start= new Button();

               abort= new Button();

               log= new TextBox();

               this.SuspendLayout();

 

               url.Location= new Point(20, 20);

               url.Text= "http://myserver/myupload.php";

               url.TextChanged += new EventHandler(on_text_changed);

               url.Width= 350;

 

               select.Location= new Point(20,url.Bottom+5);

               select.Text= "Select Files";

               select.Click += new EventHandler(on_select);

 

               start.Location= new Point(select.Right,url.Bottom+5);

               start.Text= "Upload";

               start.Click += new EventHandler(on_start);

 

               abort.Location= new Point(start.Right+10,start.Top);

               abort.Text= "Abort";

               abort.Enabled= false;

               abort.Click += new EventHandler(on_abort);

 

               log.Location= new Point(url.Left,abort.Bottom+10);

               log.Size= new Size( url.Width, 180 );

               log.Multiline= true;

               log.ScrollBars= ScrollBars.Vertical;

 

               select.Enabled= true;

               start.Enabled= false;

               abort.Enabled= false;

               url.Enabled= true;

 

               this.Controls.Add(url);

               this.Controls.Add(select);

               this.Controls.Add(start);

               this.Controls.Add(abort);

               this.Controls.Add(log);

               this.Text = "Form1";

               this.ResumeLayout(false);

          }

 

          void on_text_changed( object sender, EventArgs e )

          {

               select.Enabled= url.TextLength > 0;

          }

 

          void on_select( object sender, EventArgs e )

          {

               dialog= new FileUploaderDialog();

               dialog.Closed += new EventHandler(on_dialog_closed);

 

               string filter= "";

               filter += "Text files (*.txt)|*.txt";

               filter += "|";

               filter += "JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg";

               filter += "|";

               filter += "PNG Portable Network Graphics (*.png)|*.png";

               filter += "|";

               filter += "All files (*.*)|*.*";

 

               dialog.Filter= filter;

 

               bool outcome= dialog.ShowDialog();

 

               if( ! outcome )

               {

                    print_msg("*** dialog.show() failed");

                    return;

               }

          }

 

          void on_dialog_closed( object sender, EventArgs e )

          {

               if( dialog.Cancelled )

               {

                    dialog= null;

                    return;

               }

 

               select.Enabled= false;

               start.Enabled= true;

               uploader= dialog.Uploader;

               dialog= null;

 

               print_msg( "Selected file:\n"

                         +" Name: "+uploader.Name+"\n"

                         +" Size: "+uploader.ContentLength+" bytes\n"

                         +" Speed: "+uploader.Speed+"\n"

                         +" CreationDate: "+uploader.CreationDate+"\n"

                         +" ModificationDate: "+uploader.ModificationDate+"\n" );

          }

 

          void on_start( object sender, EventArgs e )

          {

               uploader.StateChanged += new EventHandler(on_state_changed);

               print_msg("Uploading file "+uploader.Name+" to url "+url.Text);

 

               bool outcome= uploader.Start(url.Text);

 

               if( ! outcome )

               {

                    print_msg("*** uploader.Start() failed");

                    return;

               }

 

               select.Enabled= false;

               start.Enabled= false;

               abort.Enabled= true;

               url.Enabled= false;

          }

 

          void print_msg( string msg )

          {

               Debug.WriteLine(msg);

               log.AppendText( msg + "\n" );

               log.ScrollToBottom();

          }

 

          void on_state_changed( object sender, EventArgs e )

          {

               FileUploader uploader= (FileUploader) sender;

 

               switch( uploader.State )

               {

                    case FileUploaderState.Started:

                         print_msg("Upload process has been started" );

                         break;

                    case FileUploaderState.Initiating:

                         print_msg("Initiating network connection");

                         break;

                    case FileUploaderState.Uploading:

                         print_msg("Uploading "+uploader.UploadedSize+"/"+uploader.ContentLength+"      ="+uploader.UploadedPercentage+"% ("+uploader.Speed+" bytes/s)" );

                         break;

                    case FileUploaderState.Aborted:

                         print_msg("Uploading has been aborted");

                         reset();

                         break;

                    case FileUploaderState.IOError:

                         print_msg("IO Error occurred");

                         reset();

                         break;

                    case FileUploaderState.SecurityError:

                         print_msg("Security Error occurred");

                         reset();

                         break;

                    case FileUploaderState.HttpError:

                         print_msg("Http Error occurred: status code = "+uploader.HttpStatusCode);

                         reset();

                         break;

                    case FileUploaderState.Completed:

                         print_msg("Uploading Completed: "+uploader.ContentLength+" bytes");

                         reset();

                         break;

               }

          }

 

          void on_abort( object sender, EventArgs e )

          {

               if( uploader == null )

                    return;

 

               uploader.Abort();

               reset();

          }

 

          void reset()

          {

               uploader= null;

               abort.Enabled= false;

               start.Enabled= false;

               select.Enabled= true;

               url.Enabled= true;

          }

 

          static void Main()

          {

               Application.Run( new Form1() );

          }

     }

}

Class Information

NamespaceSystem.Net
Flash LibrarySystem.scl
Flash Library Version2.0.0.1308
Silverlight LibrarySystem.Windows.Forms.dll



© 2003-2007 NETiKA Technologies. All rights reserved.