Free Download

Site Search

 

Top Download

Wednesday, September 19, 2007

HTTP Upload with Progress Monitoring Events



The Chilkat Upload component is freeware. This example demonstrates how to HTTP upload one or more files and monitor the progress with event callbacks. Formal support for the Chilkat Upload component is included with a "Chilkat Bundle" purchase.


download Download Chilkat .NET for 2.0 Framework


download Download Chilkat .NET for 1.0 / 1.1 Framework



//  

// Progress monitor callback -- called each time the percentage completion
// updates to a larger value.

public void OnPercentDone(object source, Chilkat.PercentDoneEventArgs args)
{
// args.PercentDone is an integer value between 1 and 100.
progressBar1.Value = args.PercentDone;

}

// The Chilkat.Upload.HeartbeatMs property determines the interval at which
// AbortCheck events are called. If HeartbeatMs is set to 0 (the default),
// then no AbortCheck events are called. This example sets the HeartbeatMs = 100
// milliseconds.

public void OnAbortCheck(object source, Chilkat.AbortCheckEventArgs args)
{
// Update progressBar2 so we can visually see the heartbeat.
if (progressBar2.Value > 90)
{
progressBar2.Value = 0;
}
else
{
progressBar2.Value += 10;
}

// Handle UI events to keep the user-interface responsive.

System.Windows.Forms.Application.DoEvents();

// To abort the upload while in progress, set the
// args.Abort property = true, like this:
// args.Abort = true;

}

// Demonstrates a BlockingUpload with progress monitoring events.
private void button1_Click(object sender, EventArgs e)
{
Chilkat.Upload upload = new Chilkat.Upload();

// Make sure the EnableEvents property is set to true, otherwise we
// won't receive event callbacks.

upload.EnableEvents = true;

// Call AbortCheck every 100 milliseconds.
upload.HeartbeatMs = 100;

// Setup our event callback handlers.

upload.OnPercentDone += new Chilkat.Upload.PercentDoneEventHandler(OnPercentDone);
upload.OnAbortCheck += new Chilkat.Upload.AbortCheckEventHandler(OnAbortCheck);

// To upload more than one file, call AddFileReference multiple times --
// once for each file to be uploaded.

// The formElementName is arbitrary, and can be anything.

string formElementName = "file1";
// The localFilename is a file that exists on your local filesystem.
string localFilename = "hamlet.xml";
upload.AddFileReference(formElementName, localFilename);

// The Hostname and Path properties specify the server-side
// page/program/script/CGI that will recieve and process the upload.

upload.Hostname = "www.freeaspupload.net";
upload.Path = "/freeaspupload/testUpload.asp";

// The BlockingUpload call does not return until all the files have been
// uploaded (or an error occurs or the upload is aborted by the application).

bool success = upload.BlockingUpload();
if (success == false)
{
textBox1.Text = upload.LastErrorText;
}
else
{
// When BlockingUpload returns true, it indicates that the files/data have
// been uploaded and an HTTP response status indicating success was
// received. If the success/failure of an uploaded is indicated by
// a message in the HTTP response body, you may access it via
// the ResponseBody property (which is a byte array).


textBox1.Text = System.Text.UTF8Encoding.UTF8.GetString(upload.ResponseBody);

MessageBox.Show("Success!");
}
}

No comments: