.net下软件的自动升级--上传
代码如下:
UpLoad.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace UpLoadNote
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class UpLoad : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label fname;
protected System.Web.UI.WebControls.Label fenc;
protected System.Web.UI.WebControls.Label fsize;
protected System.Web.UI.WebControls.Label LabelError;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(LabelError.Text != "")
{
LabelError.Text = "";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.ServerClick += new System.EventHandler(this.Button1_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_ServerClick(object sender, System.EventArgs e)
{
//检查上传文件不为空
if(myFile.PostedFile.FileName !="")
{
string FullName = myFile.PostedFile.FileName;
//得到这个文件的相关属性:文件名,文件类型,文件大小
this.fname.Text = myFile.PostedFile.FileName;
this.fenc.Text = myFile.PostedFile.ContentType;
this.fsize.Text = myFile.PostedFile.ContentLength.ToString() + “ bytes“;
//取得文件名(抱括路径)里最后一个"\"的索引
int j = FullName.LastIndexOf("\\");
//取得文件名
string SimpleName = FullName.Substring(j);
//保存文件到你所要的目录,这里是IIS根目录下的UpLoadNote目录.你可以改变.
//用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"为转义字符,必须用"\\"代替
string FilePath = Server.MapPath("\\UpLoadNote");
//myFile.PostedFile.SaveAs("D:\\test\\aa.doc");
myFile.PostedFile.SaveAs(FilePath + SimpleName);
// Response.Write(Server.MachineName);
}
else
{
LabelError.Text = "请选择要上传的文件!";
}
}
}
}
UpLoad.aspx
<%@ Page language="c#" debug="true" Codebehind="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="UpLoadNote.UpLoad" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>文件上传</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">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="uploderform" action="Upload.aspx" method="post" runat="server">
<table cellSpacing="2" cellPadding="2" align="center" border="1" width="580" height="400">
<tr>
<td><asp:Label ID="LabelError" Runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<table width="564">
<tr>
<td>选择要上传的文件:</td>
</tr>
<tr>
<td><input id="myFile" type="file" name="myFile" runat="server" style="WIDTH: 480px; HEIGHT: 22px"
size="60">
</td>
</tr>
<tr>
<td><input id="Button1" type="button" value="上 传" name="Button1" runat="server"></td>
</tr>
</table>
<br>
<table width="564">
<tr>
<td width="200"><b>文件资料</b></td>
<td> </td>
</tr>
<tr>
<td>文件名 :</td>
<td><asp:label id="fname" runat="server" text=""></asp:label></td>
</tr>
<tr>
<td>文件类型 :</td>
<td><asp:label id="fenc" runat="server"></asp:label></td>
</tr>
<tr>
<td>文件大小 :</td>
<td><asp:label id="fsize" runat="server"></asp:label></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>
2004年11月5日9:28:02