项目管理开发

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
   做一winform control
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();
        }

    }

}

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

 

(二)把winform control嵌入到IE中

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

    <HEAD>

        <title>WebForm1</title>

        <META content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

        <META content="C#" name="CODE_LANGUAGE">

        <META content="JavaScript" name="vs_defaultClientScript">

        <META content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

        <script language="javascript">

    function dd()

    {

   

    //hw.setAttribute("Uri","http://10.126.65.96/WebApplication2");设置控件的Uri属性

    alert(hw.getAttribute("Uri"));//得到控件的Uri属性

   

   

    }

        </script>

    </HEAD>

    <BODY>

        <FORM id="Form1" method="post" runat="server">

            <FONT face="宋体"></FONT>

        </FORM>

        <P>

            <OBJECT id="hw" style="WIDTH: 664px; HEIGHT: 488px" classid="http:MultiFileUpload.dll#MultiFileUpload.Demo"

                VIEWASTEXT>

            </OBJECT>

        </P>

        <P><INPUT id="ddd" type="button" onclick="dd()" value="点击查看属性"></P>

    </BODY>

</HTML>
在<object>标签的classid 属性中,我们指定第一步创建的那个组件的路径以及组件完整限定名。整个完整名称包括命名空间和整个类名。我们可以在上面的代码中清楚的看到组件名和限定名用#分割。这两部分参数组合在一起用来标识控件的唯一性。当然可以通过取名唯一id为hw来编写客户端脚本,与winform控件进行交互;要是用以前的activex控件,必须在服务器端和客户端注册安装此activex控件,现在这些工作都可以省去,不得不说这是.net的一大进步。

(三)配置虚拟路径   
有了HTML页面,现在来建立一个WebApplication2的虚拟目录,实际目录在C:\Inetpub\wwwroot\WebApplication2,该目录包含了MultiFileUpload.dll和WebForm1.aspx文件。当设置虚拟目录的时候,需要注意的是设置执行权限为纯脚本”。如果设置成脚本和可执行脚本”的权限。那控件可能不会正常工作(我在IIS6下设置成“脚本和可执行脚本,不正常)。你可以打开虚拟目录的属性来确认设置,设置窗口如下:
    

在这里因为要向服务器上传文件所以要把“写入”权限钩上。

(三)设置代码访问权限  

如果不访问本地磁盘和注册表的控件,可以正确显示;由于我们的控件要访问本地磁盘文件,所以要通过“管理工具-〉.net配置向导”放大权限:

1, 打开配置工具

 

2, 点击“调整.net安全性”

 

3, 选择“对此计算机进行更改”或“仅对当前用户进行更改”都可,点击“下一步”

 

4, 调整本地Intranet的权限为“完全信任”,单击“下一步”


5, 
单击“完成”,至此完成客户端安全性配置。

(四)访问winform控件

 

选择上传的文件

 


上传文件

 


通过javascript和控件交互

 


(五)总结

IE中使用窗体控件时,你需要意识到他的优点和局限。主要的优点包括: 
 1.通过Web实现丰富的动态界面。 
 2..NET技术的无缝结合(winform control 和IE)。 
 3.优于Java applets的表现。  
限制包括: 
1
.客户端需要是Windows操作系统 
2
.需要IE6.0的支持 
3
.客户端需要安装.NET运行时环境 
4
.服务器端需要是Window2000和IIS5.0或者更高版本 
由于,在windows xp及以后的版本中都预装.Net framework,所以客户端的.net运行环境无需担心;但是对于每个客户端要放大权限确实是个麻烦的问题,我现在没有很好的解决方法,如果谁有的话,请赐教。

posted on 2008-10-28 15:03  tiger534  阅读(779)  评论(1编辑  收藏  举报