FtpUploader For CSharp示例

使用示例步骤:

1.注册XproerWebPlug控件

2.添加引用

3.运行示例

 

注册XproerWebPlug控件

可在CMD模式下使用命令注册。

开始->运行->输入命令:regsvr32 c:\XproerWebPlug.dll

 

添加引用

1.

FtpUploader For CSharp示例

 

FtpUploader For CSharp示例

 

示例代码:

 

代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  XproerWebPlugLib;

namespace  Demo
{
    
public   delegate   void  TransferDataDelegate( uint  taskID,  string  speed,  string  transferedLen,  string  percent,  string  time);
    
public   delegate   void  TransferDataDelegateText( string  txt);
    
public   delegate   void  TransferDataDelegateProcessBar( string  txt);

    
public   partial   class  Form1 : Form
    {
        
enum  UploaderErrorState
        {
            FtpWASStartupError 
=   0 , // 启动网络环境错误
            FtpSocketError, // SOCKET错误
            FtpErrorOpenSocket, // 打开SOCKET错误
            FtpErrorCreateSocket, // 创建SOCKET错误
            FtpConnectError, // 连接服务器错误
            FtpErrorSendUser, // 发送用户名错误
            FtpErrorSendPass, // 发送密码错误
            FtpErrorSendData, // 发送数据错误
            FtpErrorUnSetLocalFilePath, // 未设置本地文件
            FtpErrorUnSetRemoteFileName, // 未设置远程文件名称
            FtpErrorUnSetFtpIP, // 未设置服务器IP
            FtpErrorUnSetFtpUserName, // 未设置服务用户名
            FtpErrorUnSetFtpUserPass, // 未设置服务器密码
            FtpErrorLocalFileUnExist, // 本地文件不存在
            FtpErrorReadLocalFile, // 不能读取本地文件
            FtpErrorWriteLocalFile, // 向本地文件写入数据失败
            FtpErrorWriteRemoteFile, // 向服务器写入数据失败
            FtpErrorNetCommand, // 网络命令错误
            FtpErrorRemoteFileNotExist, // 远程文件不存在
            LocalFileEmpty  =   19 ,     // 本地文件名称为空
            LocalFileLarge,     // 本地文件大小超过1G
            RemoteFileEmpty,     // 远程文件名称为空
            NamePass,         // 用户名或密码错误
            IpIllegality         // IP未授权
        }

        FileUploaderClass m_Uploader;
// 申明上传对象

        
public  Form1()
        {
            InitializeComponent();
        }

        
private   void  Form1_Load( object  sender, EventArgs e)
        {
        }

        
void  uploader_TransferStop( uint  taskID)
        {
            
this .LabelState.Text  =   " 传输已停止 " ;
        }

        
void  uploader_TransferError( uint  taskID,  uint  errCode)
        {
            UploaderErrorState state 
=  (UploaderErrorState)errCode;
            
switch  (state)
            { 
                
case  UploaderErrorState.FtpConnectError:
                    
break ;
                
case  UploaderErrorState.FtpErrorCreateSocket:
                    
break ;
                
case  UploaderErrorState.FtpErrorReadLocalFile:
                    
break ;
                
case  UploaderErrorState.FtpErrorSendData:
                    
break ;
                
case  UploaderErrorState.FtpErrorSendPass:
                    
break ;
                
case  UploaderErrorState.FtpErrorNetCommand:
                    
this .LabelState.Text  =   " 发送命令失败 " ;
                    
break ;
                
case  UploaderErrorState.FtpErrorWriteRemoteFile:
                    
this .LabelState.Text  =   " 向服务器写入数据失败 " ;
                    
break ;
                
case  UploaderErrorState.FtpSocketError:
                    
this .LabelState.Text  =   " SOCKET错误 " ;
                    
break ;
                
case  UploaderErrorState.LocalFileEmpty:
                    
this .LabelState.Text  =   " 本地文件名称为空 " ;
                    
break ;
                
case  UploaderErrorState.FtpErrorRemoteFileNotExist:
                    
this .LabelState.Text  =   " 远程文件不存在 " ;
                    
break ;
                
case  UploaderErrorState.FtpErrorUnSetRemoteFileName:
                    
this .LabelState.Text  =   " 未设置远程文件名称 " ;
                    
break ;
                
case  UploaderErrorState.LocalFileLarge:
                    
this .LabelState.Text  =   " 本地文件大小超过1G " ;
                    
break ;
                
case  UploaderErrorState.RemoteFileEmpty:
                    
this .LabelState.Text  =   " 远程文件名称为空 " ;
                    
break ;
                
case  UploaderErrorState.NamePass:
                    
this .LabelState.Text  =   " 用户名或密码错误 " ;
                    
break ;
                
case  UploaderErrorState.IpIllegality:
                    
this .LabelState.Text  =   " IP未授权 " ;
                    
break ;
            }
        }

        
void  UpdateSpeed( string  txt) {  this .LabelSpeed.Text  =   " 速度: "   +  txt; }
        
void  UpdateTime( string  txt) {  this .LabelTime.Text  =   " 剩余时间: "   +  txt; }
        
void  UpdatePostedLength( string  txt) {  this .LabelPostedLength.Text  =   " 已上传: "   +  txt; }
        
void  UpdateProcess( string  txt) {  this .ProceBarPost.Value  =   int .Parse(txt);}
        
void  UpdateState( string  txt) {  this .LabelState.Text  =  txt; }

        
// COM线程调用,所以控件类需要使用代理类
         void  uploader_TransferData( uint  taskID,  string  speed,  string  transferedLen,  string  percent,  string  time)
        {
            
this .LabelPostedLength.BeginInvoke( new  TransferDataDelegateText(UpdatePostedLength),  new   object [] { transferedLen });
            
this .LabelSpeed.BeginInvoke( new  TransferDataDelegateText(UpdateSpeed),  new   object [] { speed });
            
this .LabelTime.BeginInvoke( new  TransferDataDelegateText(UpdateTime),  new   object [] { time });
            
this .ProceBarPost.BeginInvoke( new  TransferDataDelegateProcessBar(UpdateProcess),  new   object [] { percent });
        }

        
void  uploader_TransferConnectedError( uint  taskID)
        {
            
this .LabelState.Text  =   " 连接服务器失败 " ;
        }

        
void  uploader_TransferConnected( uint  taskID)
        {
            
this .LabelState.Text  =   " 连接服务器成功 " ;
        }

        
// COM线程调用,所以需要使用代理类
         void  uploader_TransferComplete( uint  taskID)
        {
            
this .LabelState.BeginInvoke( new  TransferDataDelegateText(UpdateState),  new   object [] {  " 文件上传完毕 "  });
        }

        
private   void  BtnUpload_Click( object  sender, EventArgs e)
        {
            
if  ( this .EditFile.Text.Length  <   1 return ;
            
this .ProceBarPost.Maximum  =   100 ;
            
this .ProceBarPost.Minimum  =   0 ;
            
this .ProceBarPost.Step  =   1 ;
            
this .ProceBarPost.Value  =   0 ; // 重置进度条
             this .m_Uploader  =   new  FileUploaderClass();
            
// 连接事件
             this .m_Uploader.TransferComplete  +=   new  _IFileUploaderEvents_TransferCompleteEventHandler(uploader_TransferComplete);
            
this .m_Uploader.TransferConnected  +=   new  _IFileUploaderEvents_TransferConnectedEventHandler(uploader_TransferConnected);
            
this .m_Uploader.TransferConnectedError  +=   new  _IFileUploaderEvents_TransferConnectedErrorEventHandler(uploader_TransferConnectedError);
            
this .m_Uploader.TransferData  +=   new  _IFileUploaderEvents_TransferDataEventHandler( new  TransferDataDelegate(uploader_TransferData)); // 不加代理提示访问失败
             this .m_Uploader.TransferError  +=   new  _IFileUploaderEvents_TransferErrorEventHandler(uploader_TransferError);
            
this .m_Uploader.TransferStop  +=   new  _IFileUploaderEvents_TransferStopEventHandler(uploader_TransferStop);
            
this .m_Uploader.FileID  =   1 ;
            
this .m_Uploader.LocalFile  =   " d:\\360se_3.2.exe " ;
            
this .m_Uploader.RemoteFile  =   " 360se_3.2.exe " ;
            
this .m_Uploader.IP  =   " 127.0.0.1 " ;
            
this .m_Uploader.UserName  =   " test " ;
            
this .m_Uploader.UserPass  =   " test " ;
            
this .m_Uploader.Post();
        }

        
private   void  BtnStop_Click( object  sender, EventArgs e)
        {
            
this .m_Uploader.Stop();
        }
    }
}

 

 效果图:

FtpUploader For CSharp示例

你可能感兴趣的:(upload)