做一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;
}
}
组件设计器生成的代码#region 组件设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Demo));
this.panel1 = new System.Windows.Forms.Panel();
this.splitter3 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.splitter2 = new System.Windows.Forms.Splitter();
this.button1 = new System.Windows.Forms.Button();
this.splitter1 = new System.Windows.Forms.Splitter();
this.label1 = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.splitter4 = new System.Windows.Forms.Splitter();
this.panel3 = new System.Windows.Forms.Panel();
this.listBox1 = new System.Windows.Forms.ListBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.splitter4);
this.panel1.Controls.Add(this.progressBar1);
this.panel1.Controls.Add(this.splitter3);
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.splitter1);
this.panel1.Controls.Add(this.label1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(568, 376);
this.panel1.TabIndex = 0;
//
// splitter3
//
this.splitter3.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter3.Location = new System.Drawing.Point(72, 43);
this.splitter3.Name = "splitter3";
this.splitter3.Size = new System.Drawing.Size(494, 3);
this.splitter3.TabIndex = 4;
this.splitter3.TabStop = false;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.splitter2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
this.panel2.Location = new System.Drawing.Point(0, 43);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(72, 331);
this.panel2.TabIndex = 2;
//
// button2
//
this.button2.Dock = System.Windows.Forms.DockStyle.Fill;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(0, 155);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 174);
this.button2.TabIndex = 2;
this.button2.Text = "上传文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// splitter2
//
this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter2.Location = new System.Drawing.Point(0, 152);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(70, 3);
this.splitter2.TabIndex = 1;
this.splitter2.TabStop = false;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Top;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 152);
this.button1.TabIndex = 0;
this.button1.Text = "选择文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 40);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(566, 3);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(566, 40);
this.label1.TabIndex = 0;
this.label1.Text = "多文件上传";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(72, 351);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(494, 23);
this.progressBar1.TabIndex = 5;
//
// splitter4
//
this.splitter4.Dock = System.Windows.Forms.DockStyle.Bottom;
this.splitter4.Location = new System.Drawing.Point(72, 349);
this.splitter4.Name = "splitter4";
this.splitter4.Size = new System.Drawing.Size(494, 2);
this.splitter4.TabIndex = 6;
this.splitter4.TabStop = false;
//
// panel3
//
this.panel3.Controls.Add(this.listBox1);
this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel3.Location = new System.Drawing.Point(72, 46);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(494, 303);
this.panel3.TabIndex = 7;
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(494, 292);
this.listBox1.TabIndex = 0;
//
// Demo
//
this.Controls.Add(this.panel1);
this.Name = "Demo";
this.Size = new System.Drawing.Size(568, 376);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
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();
做一存取凭据#region 做一存取凭据
//NetworkCredential myCred = new NetworkCredential("Administrator","11");//
//CredentialCache myCache = new CredentialCache();
//myCache.Add(new Uri(this.uri), "Basic", myCred);
//myWebClient.Credentials =myCache;
#endregion
//使用默认的权限
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运行环境无需担心;但是对于每个客户端要放大权限确实是个麻烦的问题,我现在没有很好的解决方法,如果谁有的话,请赐教。