📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Security;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Dime;
namespace BufferedUploadWin
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.Button btnUpload;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.DomainUpDown dudChunkSize;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ToolTip toolTip1; // the the cursor icon embedded in the status bar
private BufferedUpload ws;
private DateTime startTime;
private BufferedUploadWin.FormBar formBar;
private int retryCount = 20; // number of times to retry a download. gets decremented each time.
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.ShowDialog();
this.textBox1.Text = this.openFileDialog1.FileName;
this.btnUpload.Enabled = (this.textBox1.Text != "");
}
private void btnUpload_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text == "")
{
this.formBar.UpdateStatusBarMessage("No file selected");
return;
}
this.formBar.UpdateStatusBarMessage("Initialising file upload...");
this.btnCancel.Enabled = true;
this.btnUpload.Enabled = false;
this.formBar.progressBar.Value = 0;
this.formBar.progressBar.Maximum = 100;
this.formBar.progressBar.Visible = true;
this.Cursor = Cursors.WaitCursor;
ws = new BufferedUpload();
try
{
ws.ChunkSize = Int32.Parse(this.dudChunkSize.Text)*1000; // kilobytes
}
catch
{
MessageBox.Show("Error: Enter numbers only in the chunk size box");
return;
}
ws.ProgressChanged += new ProgressChangedEventHandler(Upload_ProgressChanged);
ws.StateChanged += new EventHandler(Upload_StateChanged);
this.startTime = DateTime.Now; // begin a timer
ws.BeginUploadChunks(this.textBox1.Text);
}
private void Upload_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// the progress bar is set to max 100, not the number of total bytes, because this can be bigger than the max size of an int
this.formBar.progressBar.Value = e.PercentComplete;
this.formBar.UpdateStatusBarMessage(BufferedUpload.GetFileSize(ws.SentBytes) + " / " + BufferedUpload.GetFileSize(ws.Filesize));
}
private void Upload_StateChanged(object sender, EventArgs e)
{
switch(ws.State)
{
case UploadState.Completed:
TimeSpan duration = DateTime.Now - this.startTime;
this.ResetUi();
this.formBar.UpdateStatusBarMessage("Upload completed in " + string.Format("{0:d} minutes, {1:d} seconds", duration.Minutes,duration.Seconds));
break;
case UploadState.Failed:
if(this.retryCount > 0)
{
Retry retryForm = new Retry();
retryForm.Init("An error occured during the upload:\n" + this.ws.LastError.Message, 5, this.retryCount, new Retry.InitDelegate(this.ws.Retry));
DialogResult result = retryForm.ShowDialog();
if(result == DialogResult.Cancel)
this.btnCancel.PerformClick();
else // user said Yes
retryCount--;
}
else
{
this.ResetUi();
this.formBar.UpdateStatusBarMessage("Failed");
MessageBox.Show("Error: " + (sender as BufferedUpload).LastError.Message, "Upload failed");
}
break;
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
try
{
if(this.ws != null && this.ws.State == UploadState.Running || this.ws.State == UploadState.Waiting)
{
ws.ProgressChanged -= new ProgressChangedEventHandler(Upload_ProgressChanged);
ws.StateChanged -= new EventHandler(Upload_StateChanged);
this.ws.Cancel();
}
this.formBar.UpdateStatusBarMessage("Cancelled.");
}
catch(Exception ex)
{
this.formBar.UpdateStatusBarMessage("Cancelled, with errors: " + ex.Message);
}
this.ResetUi();
}
#region general user interface stuff
delegate void ResetUiDel();
private void ResetUi()
{
if(this.InvokeRequired)
{
// switch to UI thread
ResetUiDel del = new ResetUiDel(this.ResetUi);
BeginInvoke(del, new object[]{});
return;
}
this.formBar.progressBar.Visible = false;
this.btnUpload.Enabled = true;
this.btnCancel.Enabled = false;
this.Cursor = Cursors.Default;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
// clean up the upload instance on the web server
if(this.ws != null && this.ws.State == UploadState.Running || this.ws.State == UploadState.Waiting)
this.ws.RemoveInstance(this.ws.InstanceId);
}
catch{} // ignore
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnBrowse = new System.Windows.Forms.Button();
this.btnUpload = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btnCancel = new System.Windows.Forms.Button();
this.dudChunkSize = new System.Windows.Forms.DomainUpDown();
this.label2 = new System.Windows.Forms.Label();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.formBar = new BufferedUploadWin.FormBar();
this.SuspendLayout();
//
// btnBrowse
//
this.btnBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnBrowse.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnBrowse.Location = new System.Drawing.Point(625, 7);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.TabIndex = 0;
this.btnBrowse.Text = "Browse";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// btnUpload
//
this.btnUpload.Anchor = System.Windows.Forms.AnchorStyles.None;
this.btnUpload.Enabled = false;
this.btnUpload.Location = new System.Drawing.Point(268, 37);
this.btnUpload.Name = "btnUpload";
this.btnUpload.TabIndex = 2;
this.btnUpload.Text = "Start Upload";
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(84, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(533, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 7);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 23);
this.label1.TabIndex = 6;
this.label1.Text = "File to upload:";
//
// btnCancel
//
this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Enabled = false;
this.btnCancel.Location = new System.Drawing.Point(361, 37);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// dudChunkSize
//
this.dudChunkSize.Items.Add("4");
this.dudChunkSize.Items.Add("8");
this.dudChunkSize.Items.Add("16");
this.dudChunkSize.Items.Add("32");
this.dudChunkSize.Items.Add("64");
this.dudChunkSize.Items.Add("128");
this.dudChunkSize.Items.Add("256");
this.dudChunkSize.Items.Add("512");
this.dudChunkSize.Items.Add("1024");
this.dudChunkSize.Items.Add("2048");
this.dudChunkSize.Items.Add("4096");
this.dudChunkSize.Location = new System.Drawing.Point(98, 41);
this.dudChunkSize.Name = "dudChunkSize";
this.dudChunkSize.Size = new System.Drawing.Size(58, 20);
this.dudChunkSize.TabIndex = 9;
this.dudChunkSize.Text = "64";
//
// label2
//
this.label2.Location = new System.Drawing.Point(10, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(84, 23);
this.label2.TabIndex = 10;
this.label2.Text = "Chunk size (kb)";
//
// formBar
//
this.formBar.Dock = System.Windows.Forms.DockStyle.Bottom;
this.formBar.Location = new System.Drawing.Point(0, 71);
this.formBar.Name = "formBar";
this.formBar.Size = new System.Drawing.Size(707, 21);
this.formBar.TabIndex = 11;
//
// Form1
//
this.AcceptButton = this.btnUpload;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(707, 92);
this.Controls.Add(this.formBar);
this.Controls.Add(this.label2);
this.Controls.Add(this.dudChunkSize);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnUpload);
this.Controls.Add(this.btnBrowse);
this.Name = "Form1";
this.Text = "Chunked File Upload";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -