分享
 
 
 

借助WebService實現多線程上傳文件

王朝other·作者佚名  2008-12-13
窄屏简体版  字體: |||超大  

在WebService的幫助下,進行多線程上傳文件是非常簡單。因此我只做個簡單的例子,那麽如果想要實現此功能的朋友,可以在我的基礎上進行擴展。

首先說說服務器端,只需要提供一個能允許多線程寫文件的函數即可,具體代碼如下。view plaincopy to clipboardprint?

[WebMethod]

public bool UploadFileData( string FileName, int StartPosition, byte[] bData )

{

string strFullName = Server.MapPath( "Uploads" ) + @"\" + FileName;

FileStream fs = null;

try

{

fs = new FileStream( strFullName, FileMode.OpenOrCreate,

FileAccess.Write, FileShare.Write );

}

catch( IOException err )

{

Session["ErrorMessage"] = err.Message;

return false;

}

using( fs )

{

fs.Position = StartPosition;

fs.Write( bData, 0, bData.Length );

}

return true;

}

[WebMethod]

public bool UploadFileData( string FileName, int StartPosition, byte[] bData )

{

string strFullName = Server.MapPath( "Uploads" ) + @"\" + FileName;

FileStream fs = null;

try

{

fs = new FileStream( strFullName, FileMode.OpenOrCreate,

FileAccess.Write, FileShare.Write );

}

catch( IOException err )

{

Session["ErrorMessage"] = err.Message;

return false;

}

using( fs )

{

fs.Position = StartPosition;

fs.Write( bData, 0, bData.Length );

}

return true;

}其中「Uploads」是在服務程序所在目錄下的一個子目錄,需要設置ASPNET用戶對此目錄具有可寫權限。

相對於服務器端來說,客戶端要稍微復雜一些,因為要牽扯到多線程的問題。為了更好的傳遞參數,我用一個線程類來完成。具體如下。view plaincopy to clipboardprint?

public delegate void UploadFileData( string FileName, int StartPos, byte[] bData );

/// <summary>

/// FileThread: a class for sub-thread

/// </summary>

sealed class FileThread

{

private int nStartPos;

private int nTotalBytes;

private string strFileName;

public static UploadFileData UploadHandle;

/// <summary>

/// Constructor

/// </summary>

/// <param name="StartPos"></param>

/// <param name="TotalBytes"></param>

/// <param name="FileName"></param>

public FileThread( int StartPos, int TotalBytes, string FileName )

{

//Init thread variant

nStartPos = StartPos;

nTotalBytes = TotalBytes;

strFileName = FileName;

//Only for debug

Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}",

strFileName, nStartPos, nTotalBytes ) );

}

/// <summary>

/// Sub-thread entry function

/// </summary>

/// <param name="stateinfo"></param>

public void UploadFile( object stateinfo )

{

int nRealRead, nBufferSize;

const int BUFFER_SIZE = 10240;

using( FileStream fs = new FileStream( strFileName,

FileMode.Open, FileAccess.Read,

FileShare.Read ) )

{

string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 );

byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer

fs.Position = nStartPos;

nRealRead = 0;

do

{

nBufferSize = BUFFER_SIZE;

if( nRealRead + BUFFER_SIZE > nTotalBytes )

nBufferSize = nTotalBytes - nRealRead;

nBufferSize = fs.Read( bBuffer, 0, nBufferSize );

if( nBufferSize == BUFFER_SIZE )

UploadHandle( sName,

nRealRead + nStartPos,

bBuffer );

else if( nBufferSize > 0 )

{

//Copy data

byte[] bytData = new byte[nBufferSize];

Array.Copy( bBuffer,0, bytData, 0, nBufferSize );

UploadHandle( sName,

nRealRead + nStartPos,

bytData );

}

nRealRead += nBufferSize;

}

while( nRealRead < nTotalBytes );

}

//Release signal

ManualResetEvent mr = stateinfo as ManualResetEvent;

if( mr != null )

mr.Set();

}

}

public delegate void UploadFileData( string FileName, int StartPos, byte[] bData );

/// <summary>

/// FileThread: a class for sub-thread

/// </summary>

sealed class FileThread

{

private int nStartPos;

private int nTotalBytes;

private string strFileName;

public static UploadFileData UploadHandle;

/// <summary>

/// Constructor

/// </summary>

/// <param name="StartPos"></param>

/// <param name="TotalBytes"></param>

/// <param name="FileName"></param>

public FileThread( int StartPos, int TotalBytes, string FileName )

{

//Init thread variant

nStartPos = StartPos;

nTotalBytes = TotalBytes;

strFileName = FileName;

//Only for debug

Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}",

strFileName, nStartPos, nTotalBytes ) );

}

/// <summary>

/// Sub-thread entry function

/// </summary>

/// <param name="stateinfo"></param>

public void UploadFile( object stateinfo )

{

int nRealRead, nBufferSize;

const int BUFFER_SIZE = 10240;

using( FileStream fs = new FileStream( strFileName,

FileMode.Open, FileAccess.Read,

FileShare.Read ) )

{

string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 );

byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer

fs.Position = nStartPos;

nRealRead = 0;

do

{

nBufferSize = BUFFER_SIZE;

if( nRealRead + BUFFER_SIZE > nTotalBytes )

nBufferSize = nTotalBytes - nRealRead;

nBufferSize = fs.Read( bBuffer, 0, nBufferSize );

if( nBufferSize == BUFFER_SIZE )

UploadHandle( sName,

nRealRead + nStartPos,

bBuffer );

else if( nBufferSize > 0 )

{

//Copy data

byte[] bytData = new byte[nBufferSize];

Array.Copy( bBuffer,0, bytData, 0, nBufferSize );

UploadHandle( sName,

nRealRead + nStartPos,

bytData );

}

nRealRead += nBufferSize;

}

while( nRealRead < nTotalBytes );

}

//Release signal

ManualResetEvent mr = stateinfo as ManualResetEvent;

if( mr != null )

mr.Set();

}

}那麽在執行的時候,要創建線程類對象,並為每一個每個線程設置一個信號量,從而能在所有線程都結束的時候得到通知,大致的代碼如下。view plaincopy to clipboardprint?

FileInfo fi = new FileInfo( txtFileName.Text );

if( fi.Exists )

{

btnUpload.Enabled = false;//Avoid upload twice

//Init signals

ManualResetEvent[] events = new ManualResetEvent[5];

//Devide blocks

int nTotalBytes = (int)( fi.Length / 5 );

for( int i = 0; i < 5; i++ )

{

events[i] = new ManualResetEvent( false );

FileThread thdSub = new FileThread(

i * nTotalBytes,

( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ),

fi.FullName );

ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] );

}

//Wait for threads finished

WaitHandle.WaitAll( events );

//Reset button status

btnUpload.Enabled = true;

}

FileInfo fi = new FileInfo( txtFileName.Text );

if( fi.Exists )

{

btnUpload.Enabled = false;//Avoid upload twice

//Init signals

ManualResetEvent[] events = new ManualResetEvent[5];

//Devide blocks

int nTotalBytes = (int)( fi.Length / 5 );

for( int i = 0; i < 5; i++ )

{

events[i] = new ManualResetEvent( false );

FileThread thdSub = new FileThread(

i * nTotalBytes,

( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ),

fi.FullName );

ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] );

}

//Wait for threads finished

WaitHandle.WaitAll( events );

//Reset button status

btnUpload.Enabled = true;

}總體來說,程序還是相對比較簡單,而我也只是做了個簡單例子而已,一些細節都沒有進行處理。

如下是客戶端的完整代碼。view plaincopy to clipboardprint?

//--------------------------- Multi-thread Upload Demo ---------------------------------------

//--------------------------------------------------------------------------------------------

//---File: frmUpload

//---Description: The multi-thread upload form file to demenstrate howto use multi-thread to

// upload files

//---Author: Knight

//---Date: Oct.12, 2006

//--------------------------------------------------------------------------------------------

//---------------------------{Multi-thread Upload Demo}---------------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace CSUpload

{

using System.IO;

using System.Diagnostics;

using System.Threading;

using WSUploadFile;//Web-service reference namespace

/// <summary>

/// Summary description for Form1.

/// </summary>

public class frmUpload : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox txtFileName;

private System.Windows.Forms.Button btnBrowse;

private System.Windows.Forms.Button btnUpload;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public frmUpload()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <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.txtFileName = new System.Windows.Forms.TextBox();

this.btnBrowse = new System.Windows.Forms.Button();

this.btnUpload = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// txtFileName

//

this.txtFileName.Location = new System.Drawing.Point(16, 24);

this.txtFileName.Name = "txtFileName";

this.txtFileName.Size = new System.Drawing.Size(248, 20);

this.txtFileName.TabIndex = 0;

this.txtFileName.Text = "";

//

// btnBrowse

//

this.btnBrowse.Location = new System.Drawing.Point(272, 24);

this.btnBrowse.Name = "btnBrowse";

this.btnBrowse.TabIndex = 1;

this.btnBrowse.Text = "&Browse...";

this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);

//

// btnUpload

//

this.btnUpload.Location = new System.Drawing.Point(272, 56);

this.btnUpload.Name = "btnUpload";

this.btnUpload.TabIndex = 2;

this.btnUpload.Text = "&Upload";

this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);

//

// frmUpload

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(370, 111);

this.Controls.Add(this.btnUpload);

this.Controls.Add(this.btnBrowse);

this.Controls.Add(this.txtFileName);

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

this.MaximizeBox = false;

this.Name = "frmUpload";

this.Text = "Upload";

this.Load += new System.EventHandler(this.frmUpload_Load);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

static void Main()

{

Application.Run(new frmUpload());

}

private FileUpload myUpload = new FileUpload();

private void UploadData( string FileName, int StartPos, byte[] bData )

{

//Call web service upload

myUpload.UploadFileData( FileName, StartPos, bData );

}

private void btnUpload_Click(object sender, System.EventArgs e)

{

FileInfo fi = new FileInfo( txtFileName.Text );

if( fi.Exists )

{

btnUpload.Enabled = false;//Avoid upload twice

//Init signals

ManualResetEvent[] events = new ManualResetEvent[5];

//Devide blocks

int nTotalBytes = (int)( fi.Length / 5 );

for( int i = 0; i < 5; i++ )

{

events[i] = new ManualResetEvent( false );

FileThread thdSub = new FileThread(

i * nTotalBytes,

( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ),

fi.FullName );

ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] );

}

//Wait for threads finished

WaitHandle.WaitAll( events );

//Reset button status

btnUpload.Enabled = true;

}

}

private void frmUpload_Load(object sender, System.EventArgs e)

{

FileThread.UploadHandle = new UploadFileData( this.UploadData );

}

private void btnBrowse_Click(object sender, System.EventArgs e)

{

if( fileOpen.ShowDialog() == DialogResult.OK )

txtFileName.Text = fileOpen.FileName;

}

private OpenFileDialog fileOpen = new OpenFileDialog();

}

public delegate void UploadFileData( string FileName, int StartPos, byte[] bData );

/// <summary>

/// FileThread: a class for sub-thread

/// </summary>

sealed class FileThread

{

private int nStartPos;

private int nTotalBytes;

private string strFileName;

public static UploadFileData UploadHandle;

/// <summary>

/// Constructor

/// </summary>

/// <param name="StartPos"></param>

/// <param name="TotalBytes"></param>

/// <param name="FileName"></param>

public FileThread( int StartPos, int TotalBytes, string FileName )

{

//Init thread variant

nStartPos = StartPos;

nTotalBytes = TotalBytes;

strFileName = FileName;

//Only for debug

Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}",

strFileName, nStartPos, nTotalBytes ) );

}

/// <summary>

/// Sub-thread entry function

/// </summary>

/// <param name="stateinfo"></param>

public void UploadFile( object stateinfo )

{

int nRealRead, nBufferSize;

const int BUFFER_SIZE = 10240;

using( FileStream fs = new FileStream( strFileName,

FileMode.Open, FileAccess.Read,

FileShare.Read ) )

{

string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 );

byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer

fs.Position = nStartPos;

nRealRead = 0;

do

{

nBufferSize = BUFFER_SIZE;

if( nRealRead + BUFFER_SIZE > nTotalBytes )

nBufferSize = nTotalBytes - nRealRead;

nBufferSize = fs.Read( bBuffer, 0, nBufferSize );

if( nBufferSize == BUFFER_SIZE )

UploadHandle( sName,

nRealRead + nStartPos,

bBuffer );

else if( nBufferSize > 0 )

{

//Copy data

byte[] bytData = new byte[nBufferSize];

Array.Copy( bBuffer,0, bytData, 0, nBufferSize );

UploadHandle( sName,

nRealRead + nStartPos,

bytData );

}

nRealRead += nBufferSize;

}

while( nRealRead < nTotalBytes );

}

//Release signal

ManualResetEvent mr = stateinfo as ManualResetEvent;

if( mr != null )

mr.Set();

}

}

}

 
 
 
免責聲明:本文為網絡用戶發布,其觀點僅代表作者個人觀點,與本站無關,本站僅提供信息存儲服務。文中陳述內容未經本站證實,其真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。
  在WebService的幫助下,進行多線程上傳文件是非常簡單。因此我只做個簡單的例子,那麽如果想要實現此功能的朋友,可以在我的基礎上進行擴展。   首先說說服務器端,只需要提供一個能允許多線程寫文件的函數即可,具體代碼如下。view plaincopy to clipboardprint? [WebMethod] public bool UploadFileData( string FileName, int StartPosition, byte[] bData ) { string strFullName = Server.MapPath( "Uploads" ) + @"\" + FileName; FileStream fs = null; try { fs = new FileStream( strFullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write ); } catch( IOException err ) { Session["ErrorMessage"] = err.Message; return false; } using( fs ) { fs.Position = StartPosition; fs.Write( bData, 0, bData.Length ); } return true; } [WebMethod] public bool UploadFileData( string FileName, int StartPosition, byte[] bData ) { string strFullName = Server.MapPath( "Uploads" ) + @"\" + FileName; FileStream fs = null; try { fs = new FileStream( strFullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write ); } catch( IOException err ) { Session["ErrorMessage"] = err.Message; return false; } using( fs ) { fs.Position = StartPosition; fs.Write( bData, 0, bData.Length ); } return true; }  其中「Uploads」是在服務程序所在目錄下的一個子目錄,需要設置ASPNET用戶對此目錄具有可寫權限。   相對於服務器端來說,客戶端要稍微復雜一些,因為要牽扯到多線程的問題。為了更好的傳遞參數,我用一個線程類來完成。具體如下。view plaincopy to clipboardprint? public delegate void UploadFileData( string FileName, int StartPos, byte[] bData ); /// <summary> /// FileThread: a class for sub-thread /// </summary> sealed class FileThread { private int nStartPos; private int nTotalBytes; private string strFileName; public static UploadFileData UploadHandle; /// <summary> /// Constructor /// </summary> /// <param name="StartPos"></param> /// <param name="TotalBytes"></param> /// <param name="FileName"></param> public FileThread( int StartPos, int TotalBytes, string FileName ) { //Init thread variant nStartPos = StartPos; nTotalBytes = TotalBytes; strFileName = FileName; //Only for debug Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}", strFileName, nStartPos, nTotalBytes ) ); } /// <summary> /// Sub-thread entry function /// </summary> /// <param name="stateinfo"></param> public void UploadFile( object stateinfo ) { int nRealRead, nBufferSize; const int BUFFER_SIZE = 10240; using( FileStream fs = new FileStream( strFileName, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 ); byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer fs.Position = nStartPos; nRealRead = 0; do { nBufferSize = BUFFER_SIZE; if( nRealRead + BUFFER_SIZE > nTotalBytes ) nBufferSize = nTotalBytes - nRealRead; nBufferSize = fs.Read( bBuffer, 0, nBufferSize ); if( nBufferSize == BUFFER_SIZE ) UploadHandle( sName, nRealRead + nStartPos, bBuffer ); else if( nBufferSize > 0 ) { //Copy data byte[] bytData = new byte[nBufferSize]; Array.Copy( bBuffer,0, bytData, 0, nBufferSize ); UploadHandle( sName, nRealRead + nStartPos, bytData ); } nRealRead += nBufferSize; } while( nRealRead < nTotalBytes ); } //Release signal ManualResetEvent mr = stateinfo as ManualResetEvent; if( mr != null ) mr.Set(); } } public delegate void UploadFileData( string FileName, int StartPos, byte[] bData ); /// <summary> /// FileThread: a class for sub-thread /// </summary> sealed class FileThread { private int nStartPos; private int nTotalBytes; private string strFileName; public static UploadFileData UploadHandle; /// <summary> /// Constructor /// </summary> /// <param name="StartPos"></param> /// <param name="TotalBytes"></param> /// <param name="FileName"></param> public FileThread( int StartPos, int TotalBytes, string FileName ) { //Init thread variant nStartPos = StartPos; nTotalBytes = TotalBytes; strFileName = FileName; //Only for debug Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}", strFileName, nStartPos, nTotalBytes ) ); } /// <summary> /// Sub-thread entry function /// </summary> /// <param name="stateinfo"></param> public void UploadFile( object stateinfo ) { int nRealRead, nBufferSize; const int BUFFER_SIZE = 10240; using( FileStream fs = new FileStream( strFileName, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 ); byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer fs.Position = nStartPos; nRealRead = 0; do { nBufferSize = BUFFER_SIZE; if( nRealRead + BUFFER_SIZE > nTotalBytes ) nBufferSize = nTotalBytes - nRealRead; nBufferSize = fs.Read( bBuffer, 0, nBufferSize ); if( nBufferSize == BUFFER_SIZE ) UploadHandle( sName, nRealRead + nStartPos, bBuffer ); else if( nBufferSize > 0 ) { //Copy data byte[] bytData = new byte[nBufferSize]; Array.Copy( bBuffer,0, bytData, 0, nBufferSize ); UploadHandle( sName, nRealRead + nStartPos, bytData ); } nRealRead += nBufferSize; } while( nRealRead < nTotalBytes ); } //Release signal ManualResetEvent mr = stateinfo as ManualResetEvent; if( mr != null ) mr.Set(); } }  那麽在執行的時候,要創建線程類對象,並為每一個每個線程設置一個信號量,從而能在所有線程都結束的時候得到通知,大致的代碼如下。view plaincopy to clipboardprint? FileInfo fi = new FileInfo( txtFileName.Text ); if( fi.Exists ) { btnUpload.Enabled = false;//Avoid upload twice //Init signals ManualResetEvent[] events = new ManualResetEvent[5]; //Devide blocks int nTotalBytes = (int)( fi.Length / 5 ); for( int i = 0; i < 5; i++ ) { events[i] = new ManualResetEvent( false ); FileThread thdSub = new FileThread( i * nTotalBytes, ( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ), fi.FullName ); ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] ); } //Wait for threads finished WaitHandle.WaitAll( events ); //Reset button status btnUpload.Enabled = true; } FileInfo fi = new FileInfo( txtFileName.Text ); if( fi.Exists ) { btnUpload.Enabled = false;//Avoid upload twice //Init signals ManualResetEvent[] events = new ManualResetEvent[5]; //Devide blocks int nTotalBytes = (int)( fi.Length / 5 ); for( int i = 0; i < 5; i++ ) { events[i] = new ManualResetEvent( false ); FileThread thdSub = new FileThread( i * nTotalBytes, ( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ), fi.FullName ); ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] ); } //Wait for threads finished WaitHandle.WaitAll( events ); //Reset button status btnUpload.Enabled = true; }  總體來說,程序還是相對比較簡單,而我也只是做了個簡單例子而已,一些細節都沒有進行處理。   如下是客戶端的完整代碼。view plaincopy to clipboardprint? //--------------------------- Multi-thread Upload Demo --------------------------------------- //-------------------------------------------------------------------------------------------- //---File: frmUpload //---Description: The multi-thread upload form file to demenstrate howto use multi-thread to // upload files //---Author: Knight //---Date: Oct.12, 2006 //-------------------------------------------------------------------------------------------- //---------------------------{Multi-thread Upload Demo}--------------------------------------- using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CSUpload { using System.IO; using System.Diagnostics; using System.Threading; using WSUploadFile;//Web-service reference namespace /// <summary> /// Summary description for Form1. /// </summary> public class frmUpload : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtFileName; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.Button btnUpload; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public frmUpload() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <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.txtFileName = new System.Windows.Forms.TextBox(); this.btnBrowse = new System.Windows.Forms.Button(); this.btnUpload = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txtFileName // this.txtFileName.Location = new System.Drawing.Point(16, 24); this.txtFileName.Name = "txtFileName"; this.txtFileName.Size = new System.Drawing.Size(248, 20); this.txtFileName.TabIndex = 0; this.txtFileName.Text = ""; // // btnBrowse // this.btnBrowse.Location = new System.Drawing.Point(272, 24); this.btnBrowse.Name = "btnBrowse"; this.btnBrowse.TabIndex = 1; this.btnBrowse.Text = "&Browse..."; this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click); // // btnUpload // this.btnUpload.Location = new System.Drawing.Point(272, 56); this.btnUpload.Name = "btnUpload"; this.btnUpload.TabIndex = 2; this.btnUpload.Text = "&Upload"; this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); // // frmUpload // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(370, 111); this.Controls.Add(this.btnUpload); this.Controls.Add(this.btnBrowse); this.Controls.Add(this.txtFileName); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "frmUpload"; this.Text = "Upload"; this.Load += new System.EventHandler(this.frmUpload_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> static void Main() { Application.Run(new frmUpload()); } private FileUpload myUpload = new FileUpload(); private void UploadData( string FileName, int StartPos, byte[] bData ) { //Call web service upload myUpload.UploadFileData( FileName, StartPos, bData ); } private void btnUpload_Click(object sender, System.EventArgs e) { FileInfo fi = new FileInfo( txtFileName.Text ); if( fi.Exists ) { btnUpload.Enabled = false;//Avoid upload twice //Init signals ManualResetEvent[] events = new ManualResetEvent[5]; //Devide blocks int nTotalBytes = (int)( fi.Length / 5 ); for( int i = 0; i < 5; i++ ) { events[i] = new ManualResetEvent( false ); FileThread thdSub = new FileThread( i * nTotalBytes, ( fi.Length - i * nTotalBytes ) > nTotalBytes ? nTotalBytes:(int)( fi.Length - i * nTotalBytes ), fi.FullName ); ThreadPool.QueueUserWorkItem( new WaitCallback( thdSub.UploadFile ), events[i] ); } //Wait for threads finished WaitHandle.WaitAll( events ); //Reset button status btnUpload.Enabled = true; } } private void frmUpload_Load(object sender, System.EventArgs e) { FileThread.UploadHandle = new UploadFileData( this.UploadData ); } private void btnBrowse_Click(object sender, System.EventArgs e) { if( fileOpen.ShowDialog() == DialogResult.OK ) txtFileName.Text = fileOpen.FileName; } private OpenFileDialog fileOpen = new OpenFileDialog(); } public delegate void UploadFileData( string FileName, int StartPos, byte[] bData ); /// <summary> /// FileThread: a class for sub-thread /// </summary> sealed class FileThread { private int nStartPos; private int nTotalBytes; private string strFileName; public static UploadFileData UploadHandle; /// <summary> /// Constructor /// </summary> /// <param name="StartPos"></param> /// <param name="TotalBytes"></param> /// <param name="FileName"></param> public FileThread( int StartPos, int TotalBytes, string FileName ) { //Init thread variant nStartPos = StartPos; nTotalBytes = TotalBytes; strFileName = FileName; //Only for debug Debug.WriteLine( string.Format( "File name:{0} position: {1} total byte:{2}", strFileName, nStartPos, nTotalBytes ) ); } /// <summary> /// Sub-thread entry function /// </summary> /// <param name="stateinfo"></param> public void UploadFile( object stateinfo ) { int nRealRead, nBufferSize; const int BUFFER_SIZE = 10240; using( FileStream fs = new FileStream( strFileName, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { string sName = strFileName.Substring( strFileName.LastIndexOf( "\\" ) + 1 ); byte[] bBuffer = new byte[BUFFER_SIZE];//Init 10k buffer fs.Position = nStartPos; nRealRead = 0; do { nBufferSize = BUFFER_SIZE; if( nRealRead + BUFFER_SIZE > nTotalBytes ) nBufferSize = nTotalBytes - nRealRead; nBufferSize = fs.Read( bBuffer, 0, nBufferSize ); if( nBufferSize == BUFFER_SIZE ) UploadHandle( sName, nRealRead + nStartPos, bBuffer ); else if( nBufferSize > 0 ) { //Copy data byte[] bytData = new byte[nBufferSize]; Array.Copy( bBuffer,0, bytData, 0, nBufferSize ); UploadHandle( sName, nRealRead + nStartPos, bytData ); } nRealRead += nBufferSize; } while( nRealRead < nTotalBytes ); } //Release signal ManualResetEvent mr = stateinfo as ManualResetEvent; if( mr != null ) mr.Set(); } } }
美眾議院議長啟動對拜登的彈劾調查
 百态   2023-09-13
上海、濟南、武漢等多地出現不明墜落物
 探索   2023-09-06
印度或要將國名改為「巴拉特」
 百态   2023-09-06
男子為女友送行,買票不登機被捕
 百态   2023-08-20
手機地震預警功能怎麽開?
 干货   2023-08-06
女子4年賣2套房花700多萬做美容:不但沒變美臉,面部還出現變形
 百态   2023-08-04
住戶一樓被水淹 還衝來8頭豬
 百态   2023-07-31
女子體內爬出大量瓜子狀活蟲
 百态   2023-07-25
地球連續35年收到神秘規律性信號,網友:不要回答!
 探索   2023-07-21
全球鎵價格本周大漲27%
 探索   2023-07-09
錢都流向了那些不缺錢的人,苦都留給了能吃苦的人
 探索   2023-07-02
倩女手遊刀客魅者強控制(強混亂強眩暈強睡眠)和對應控制抗性的關系
 百态   2020-08-20
美國5月9日最新疫情:美國確診人數突破131萬
 百态   2020-05-09
荷蘭政府宣布將集體辭職
 干货   2020-04-30
倩女幽魂手遊師徒任務情義春秋猜成語答案逍遙觀:鵬程萬裏
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案神機營:射石飲羽
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案昆侖山:拔刀相助
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案天工閣:鬼斧神工
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案絲路古道:單槍匹馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:與虎謀皮
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:指鹿為馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:小鳥依人
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:千金買鄰
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:倒履相迎
 干货   2019-11-12
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有