1)aspx文件:
<%@ Page language="c#" Codebehind="AddMap.aspx.cs" AutoEventWireup="false" Inherits="ZiFengLast.SystemSet.AddMap" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MapAdmin</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">
<LINK href="../StyleSheet1.css" type="text/css" rel="stylesheet">
<script language="javascript">
function AddFile()
{
var rows=document.getElementById("rows").value
var cols=document.getElementById("cols").value
var FileList=document.getElementById("MyFile")
if(document.getElementById("MyFile").innerHTML.toString().length>200)
{
// alert(document.getElementById("MyFile").innerHTML.toString().length)
FileList.innerHTML="<font class='TagTitle'><b> 第1个地图:</b></font><input style='WIDTH: 200pxHEIGHT: 22px' type='file' size='14' name='File'>"
}
for(i=1i<cols*rowsi++)
{
j=i+1
var str ='<font class="TagTitle"><b> 第'+j+'个地图:<困肆码/b></font><input style="WIDTH: 200pxHEIGHT: 22px" type="file" size="14" name="File">'
FileList.insertAdjacentHTML("beforeEnd","<br>")
FileList.insertAdjacentHTML("beforeEnd",str)
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1"雹告 method="post" encType="multipart/form-data" runat="server">
<FONT face="宋体">
<TABLE id="Table1" style="BORDER-RIGHT: #33cc66 2px grooveBORDER-TOP: #33cc66 2px grooveZ-INDEX: 101LEFT: 136pxBORDER-LEFT: #33cc66 2px grooveBORDER-BOTTOM: #33cc66 2px groovePOSITION: absoluteTOP: 32px"
width="500" align="center">
<TBODY>
<TR height="20">
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="34%"> </TD>
</TR>
<TR>
<TD colSpan="2"> </TD>
<TD colSpan="4"><FONT class="WelcomeText" face="幼圆" color="#ff00ff" size="4"> 添加新地图</FONT></TD>
</TR>
<tr>
<td class="TagTitle" align="right" colSpan="3"><b>请选择地图规格: </b></td>
<td class="TagTitle" colSpan="2"><input id="rows" style="WIDTH: 32pxHEIGHT: 22px" type="text" size="1" value="1" runat="server"
NAME="rows">行<input id="cols" style="WIDTH: 32pxHEIGHT: 22px" type="text" size="1" value="1" runat="server"
NAME="cols">列</td>
<td colSpan="2"> <input style="WIDTH: 72pxHEIGHT: 22px" onclick="AddFile()" type="button" value="确定"></td>
</tr>
<TR>
<TD class="TagTitle" align="right" width="33%" colSpan="3"><B>请选择地图文件:</B>
</TD>
<TD class="TagTitle" width="33%" colSpan="4"></TD>
</TR>
<tr>
<td colSpan="7">
<p id="MyFile"><font class="TagTitle"><b> 第1个地图:</b></font>
<input style="WIDTH: 200pxHEIGHT: 22px" type="file" size="14" name="File">
</p>
</td>
<TR>
<TD class="TagTitle" align="right" width="33%" colSpan="3"><B>请输入地图存储名称:</B>
</TD>
<TD class="TagTitle" align="left" width="33%" colSpan="4"><INPUT id="MapName" style="WIDTH: 128pxHEIGHT: 22px" type="text" size="16" name="Password"
runat="server">
<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" CssClass="TagTitle" ErrorMessage="地图名不能为空"
Font-Names="微软雅黑" Font-Size="X-Small" ControlToValidate="MapName"></asp:requiredfieldvalidator></TD>
</TR>
<TR>
<TD class="TagTitle" style="HEIGHT: 29px" align="right" width="33%" colSpan="3"><B><asp:label id="Msg0" runat="server"></asp:label> </B>
</TD>
<TD class="TagTitle" style="HEIGHT: 29px" width="33%" colSpan="4"><span class="ErrorMessage" id="OutputSpan" runat="server"><asp:label id="Msg" runat="server"></asp:label></span></TD>
</TR>
<TR>
<TD width="11%"> </TD>
<TD width="11%"> </TD>
<TD width="22%" colSpan="4">
<asp:button id="SubmitButton" runat="server" Font-Names="微软雅黑" Font-Size="X-Small" Text="提交"
Height="26px" Width="60px"></asp:button> <INPUT id="CancelButton" style="FONT-SIZE: x-smallWIDTH: 60pxFONT-FAMILY: 微软雅黑HEIGHT: 26px"
type="reset" value="清除" name="CancelButton"></TD>
<TD width="34%">
</TD>
</TR>
</TBODY>
</TABLE>
<TABLE id="Table2" style="Z-INDEX: 102LEFT: 136pxWIDTH: 491pxPOSITION: absoluteTOP: 8pxHEIGHT: 26px"
width="491" align="center">
<TR>
<TD class="TagTitle"><A href="DeleteMap.aspx"><FONT class="WelcomeText" face="幼圆">删除已有地图</FONT></A></TD>
</TR>
</TABLE>
</FONT>
</form>
</TR></TBODY></TABLE></FONT>
</body>
</HTML>
2)后台cs文件:关键是使用Request.Files获取刚上传的文件:
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.Configuration
namespace ZiFengLast.SystemSet
{
/// <summary>
/// MapAdmin 的摘要说明。
/// </summary>
public class AddMap : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button SubmitButton
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2
protected System.Web.UI.HtmlControls.HtmlGenericControl OutputSpan
protected System.Web.UI.WebControls.Label Msg
protected System.Web.UI.WebControls.Label Msg0
protected System.Data.SqlClient.SqlConnection MapConn
protected System.Data.SqlClient.SqlCommand MapComm
protected System.Web.UI.HtmlControls.HtmlInputText rows
protected System.Web.UI.HtmlControls.HtmlInputText cols
protected System.Web.UI.HtmlControls.HtmlInputText MapName
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent()
base.OnInit(e)
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.MapConn = new System.Data.SqlClient.SqlConnection()
this.MapComm = new System.Data.SqlClient.SqlCommand()
this.SubmitButton.Click += new System.EventHandler(this.SubmitButton_Click)
//
// MapConn
//
this.MapConn.ConnectionString =System.Configuration.ConfigurationSettings.AppSettings["sqlMap"]
//
// MapComm
//
this.MapComm.Connection = this.MapConn
this.Load += new System.EventHandler(this.Page_Load)
}
#endregion
private void SubmitButton_Click(object sender, System.EventArgs e)
{
try
{
HttpFileCollection file=Request.Files
System.Text.StringBuilder FileNameArray=new System.Text.StringBuilder()
string SaveName=this.MapName.Value.Trim()
string FileExtension0=System.IO.Path.GetExtension(System.IO.Path.GetFileName(file[0].FileName))
for(int i=0i<file.Counti++)
{
HttpPostedFile PostFile=file[i]
string FileName=System.IO.Path.GetFileName(PostFile.FileName)
FileName.Replace("","_")
string FileExtension=System.IO.Path.GetExtension(FileName)
string ContentType=PostFile.ContentType.ToString()
string LastSaveName=SaveName+(i+1).ToString()+FileExtension
PostFile.SaveAs(Request.MapPath("../Map/"+LastSaveName))
FileNameArray.Append(LastSaveName)
FileNameArray.Append("")
}
this.MapComm.CommandText="Select [Map].* from [Map] where MapName='"+SaveName+"'"
this.MapConn.Open()
System.Data.SqlClient.SqlDataReader rd=this.MapComm.ExecuteReader()
if(rd.Read())
{
this.MapConn.Close()
this.MapConn.Dispose()
this.MapComm.Dispose()
rd.Close()
Msg0.Text="<font class=ErrorMessage>上传文件失败!</font>"
Msg.Text="<font class=ErrorMessage>已有该地图名称,请指定另外的名称</font>"
}
else
{
rd.Close()
this.MapComm.CommandText="insert into [Map](MapName,MapRows,MapCols,MapFileNames) Values('"+SaveName+"',"+int.Parse(rows.Value)+","+int.Parse(cols.Value)+",'"+FileNameArray+"')"
//Response.Write(MapComm.CommandText)
this.MapComm.ExecuteNonQuery()
this.MapConn.Close()
this.MapConn.Dispose()
this.MapComm.Dispose()
}
Msg0.Text="<font class=ErrorMessage>上传文件成功!</font>"
Msg.Text="<font class=ErrorMessage>共计上传文件"+file.Count.ToString()+"个"+"<br>"+FileNameArray+"</font>"
}
catch(Exception ee)
{
Msg0.Text="<font class=ErrorMessage>上传文件失败!</font>"
Msg.Text="<font class=ErrorMessage>"+ee.ToString()+"</font>"
}
}
}
}
代表通过HTTP协议用POST方法上传胡消氏上来的文件。这是一个类,using System.Web,在System.Web.dll程序集里面。
注意,它只是引用上传的文件的一个临时文件,文件名裤散可能是一堆乱码什么的
如果要永久保存上传上来的那个文件,
请使用它的
SaveAs 方法。
另外桥闭,它直接继承自System.Object,所以成员方法也不多。呵呵!
写一个扩展方慧埋法,比如:
List<HttpPostedFile> ToHttpPostedFileList(
(collection ==
歼做ArgumentNullException(
httpPostedFileList =
前改蚂List<HttpPostedFile>
(
i =
i < collection.Count i++
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)