.net中activex的替代技术:winform control(一)

来源:互联网 发布:matlab 产生矩阵 编辑:程序博客网 时间:2024/06/14 19:29

.net中activex的替代技术:winform control(一)

        我想做用vc做一activex控件对初始编程的人来说可能不是一件容易的事吧,在.net出现之后,有更简单的技术来替代它,那就是winform control,它可以把做好的winform control直接嵌入到IE中,对于访问本地磁盘和注册表的winform control,可能要重新设置安全策略。我们就拿多文件上传为例,进行讲述。

(一)   做一winform control
上面的代码中,公布了Uri和ServerFolder属性:Uri指定上传的url地址,ServerFolder指定存放的上传文件的文件夹;这俩个属性都是可读可写的,可以在网页中通过javascript和用户交互。到此为止,上传的winform控件就完成了,非常简单。(未完待续)

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
namespace MultiFileUpload
{
    
/// <summary>
    
/// UserControl1 的摘要说明。
    
/// </summary>

    public class Demo : System.Windows.Forms.UserControl
    
{
        
private System.Windows.Forms.Panel panel1;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Splitter splitter1;
        
private System.Windows.Forms.Panel panel2;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Splitter splitter2;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.OpenFileDialog openFileDialog1;
        
public WebClient wc=new WebClient();
        
private string filestr="";
        
private string[] FileListArr;
        
private System.Windows.Forms.Splitter splitter3;
        
private System.Windows.Forms.ProgressBar progressBar1;
        
private System.Windows.Forms.Splitter splitter4;
        
private System.Windows.Forms.Panel panel3;
        
private System.Windows.Forms.ListBox listBox1;
        
private string uri="http://10.126.65.96/WebApplication2";//根据自己的情况设置
        private string serverfolder="usercontrols";//根据自己的情况设置
        /// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Demo()
        
{
            
// 该调用是 Windows.Forms 窗体设计器所必需的。
            InitializeComponent();

            
// TODO: 在 InitComponent 调用后添加任何初始化

        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if( components != null )
                    components.Dispose();
            }

            
base.Dispose( disposing );
        }

        
        
public string Uri
        
{
            
set
            
{
            
this.uri=value;
            }

            
get
            
{
            
return this.uri;
            }

        }

        
public string ServerFolder
        
{
            
set
            
{
            
this.serverfolder=value;
            }

            
get
            
{
            
return this.serverfolder;
            }

        }

        
组件设计器生成的代码
        
public string GetUri()
        
{
        
return this.uri;
        }

        
public void SendFiles()
        
{
            
            
char[] ch=new char[1];
            ch[
0]='/';
            
string[] arrstr=this.filestr.Split(ch);
            
            WebClient myWebClient 
= new WebClient(); 
            
做一存取凭据
            
//使用默认的权限              
            myWebClient.Credentials =CredentialCache.DefaultCredentials; 
            
int count=this.listBox1.Items.Count;
            
this.progressBar1.Maximum=10*count;
            
for(int i=0;i<arrstr.Length;i++)
            
{
                
try 
                

                
                    
string fileNamePath=arrstr[i];
                    
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//"+ 1); 
                    
string uriString = this.uri+"/"+this.serverfolder+"/" + fileName;//指定上传得路径 
                    
// 要上传的文件 
                    FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); 
                    
//FileStream fs = OpenFile(); 
                    BinaryReader r = new BinaryReader(fs); 
            
                    
//使用UploadFile方法可以用下面的格式 
                    
//                    myWebClient.UploadFile(uriString,"PUT",fileNamePath); 
                    
//                    MessageBox.Show(uriString);
                    byte[] postArray = r.ReadBytes((int)fs.Length); 
                    Stream postStream 
= myWebClient.OpenWrite(uriString,"PUT"); 
                    
if(postStream.CanWrite) 
                    

                        postStream.Write(postArray,
0,postArray.Length); 
                        
this.progressBar1.Value=(i+1)*10;
                    }
 
                    
else 
                    

                        
                        MessageBox.Show( 
"文件目前不可写!"); 
                    }
 
                    postStream.Close(); 
                 
                
                }
 
                
catch(WebException errMsg) 
                

                    
                    MessageBox.Show(
"上传失败:" + errMsg.Message); 
                    
break;
                }
 
            }

            
if(this.progressBar1.Value==this.progressBar1.Maximum)
            
{
                MessageBox.Show(
"成功");
            }

            
else
            
{
            MessageBox.Show(
"传送中出现错误!");
            }

        }

        
private void button1_Click(object sender, System.EventArgs e)
        
{
            OpenFileDialog    openFileDialog1 
= new System.Windows.Forms.OpenFileDialog();
            openFileDialog1.Multiselect 
= true;
            openFileDialog1.RestoreDirectory 
= true;
            openFileDialog1.Filter
="dwg文件|*.dwg";
            openFileDialog1.ShowDialog();
            
this.FileListArr=openFileDialog1.FileNames;
            
this.listBox1.Items.Clear();
            
for(int i=0;i<this.FileListArr.Length;i++)
            
{
                
this.listBox1.Items.Add(this.FileListArr[i]);
            }

            
this.filestr="";
            
for(int i=0;i<this.FileListArr.Length;i++)
            
{
                
if(this.filestr=="")
                
{
                    
this.filestr=this.FileListArr[i];

                }

                
else
                
{
                    
this.filestr=this.filestr+"/"+this.FileListArr[i];
                }

            }

        }


        
private void button2_Click(object sender, System.EventArgs e)
        
{
            
this.SendFiles();
        }

    }

}

 

 
原创粉丝点击