前台:
<form id="upload" method="post" encType="multipart/form-data" runat="server">
<table class="border" id="uploadtable" height="99%" cellSpacing="0" cellPadding="0" width="98%"
align="center" border="0" runat="server">
<tr align="center" height="18">
<td><b>文件上传</b></td>
</tr>
<tr height="150">
<td class="tdbg" vAlign="top" align="center">
<table cellSpacing="1" cellPadding="4" align="center">
<tr>
<td width="30%">选择上传文件:
</td>
<td><input class="text" id="myFile" type="file" size="25" name="myFile" runat="server">
</td>
</tr>
<tr>
<td align="center" colSpan="2"><asp:radiobuttonlist id="addto" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="downfile" Selected="True">文件</asp:ListItem>
<asp:ListItem Value="img">图片</asp:ListItem>
</asp:radiobuttonlist></td>
</tr>
<tr>
<td>文件说明:</td>
<td><asp:textbox id="aboutfile" runat="server" Columns="20" CssClass="text"></asp:textbox></td>
</tr>
<tr>
<td align="center" colSpan="2"><asp:button id="Submit" onclick="Submit_OnClick" runat="server" text="上 传"></asp:button><input type="reset" value="重 置" name="reset">
</td>
</tr>
</table>
<br>
<br>
<asp:label id="myLabel" runat="server" text="" width="100%"></asp:label><br>
</td>
</tr>
</table>
<table class="border" id="successtable" height="99%" cellSpacing="0" cellPadding="0" width="98%"
align="center" border="0" runat="server">
<tr height="18">
<td class="title" align="center"><b>上 传 成 功</b></td>
</tr>
<tr height="150">
<td class="tdbg" vAlign="top">
<span id="successinfo" runat="server"></span>
<div align="center">
<input type="button" value="继续上传" name="goon" onclick="javascript:window.history.back();">
<input type="button" value="关 闭" name="close" onclick="javascript:window.close();">
</div>
</td>
</tr>
</table>
</form>
后台:
public string errmsg = "";
protected bool founderr = false;
![]()
protected System.Web.UI.WebControls.RadioButtonList addto;
protected System.Web.UI.WebControls.TextBox aboutfile;
protected System.Web.UI.WebControls.Button Submit;
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.HtmlControls.HtmlTable uploadtable;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.HtmlControls.HtmlTable successtable;
protected System.Web.UI.HtmlControls.HtmlGenericControl successinfo;
![]()
public string uploadtype="txt,gif,jpg,bmp,zip,rar,mht,swf,rm,mp3,wav,doc,ppt,xls";
public string uploadsize="6000000";
![]()
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
uploadtable.Visible = true;
successtable.Visible = false;
myLabel.Text = "<blockquote><div align=left>文件类型:" + uploadtype + "<br>文件大小:" + uploadsize + " 字节</div></blockquote>";
}
![]()
protected void Submit_OnClick(object sender, System.EventArgs e)
{
string userid ="zm";
if(myFile.PostedFile != null)
{
string str_body = "";
HttpPostedFile mFile = myFile.PostedFile;
int fileSize = mFile.ContentLength;//上传文件大小
if(fileSize == 0)
{
myLabel.Text = "<font color=red>请选择要上传的文件!</font>";
return;
}
else if(fileSize > int.Parse(uploadsize))
{
myLabel.Text = "<font color=red>您所上传的文件过大,请重新上传!</font>";
return;
}
string fileExt = mFile.FileName.Substring(mFile.FileName.Length - 3).ToLower().Replace(".","");
if(uploadtype.ToLower() != "all")
{
string uploadType = uploadtype.ToLower();
string[] array = uploadType.Split(new char[]{','});
bool cannot = true;
for(int i = 0; i < array.Length; i++)
{
if(fileExt == array.GetValue(i).ToString())
{
cannot = false;
}
}
if(cannot)
{
myLabel.Text = "<font color=red>" + fileExt + " 文件不是系统所允许上传的文件类型!</font>";
return;
}
}
try
{
byte[] mFileByte = new Byte[fileSize];
mFile.InputStream.Read(mFileByte,0,fileSize);
![]()
string mFilename = ClientID + System.IO.Path.GetFileName(mFile.FileName);//上传文件名称
str_body += "上传文件名称: " + System.IO.Path.GetFileName(mFile.FileName) + " <br>";
str_body += "上传文件大小: " + fileSize + " 字节<br>";
str_body += "上传文件类型: " + mFile.ContentType + " <br>";
![]()
string filePath = ""; //上传文件路径
switch(addto.SelectedItem.Value)
{
case "img":
filePath = "uploadImages/";
break;
case "downfile":
filePath = "uploadFiles/";
break;
}
string y = DateTime.Now.Year.ToString();
string m = DateTime.Now.Month.ToString();
string d = DateTime.Now.Day.ToString();
string h = DateTime.Now.Hour.ToString();
string n =DateTime.Now.Minute.ToString();
string s = DateTime.Now.Second.ToString();
string Filename = y + m + d + h + n + s;
Random r = new Random();
Filename = Filename + r.Next(10000);
Filename = userid + Filename + "." + fileExt;
str_body += "上传后的文件名: " + Filename + "<br>";
str_body += "文件相对当前页面路径: " + filePath;
![]()
System.IO.FileStream saveFile = new System.IO.FileStream(Server.MapPath(filePath + Filename),System.IO.FileMode.Create);
// 保存文件
saveFile.Write(mFileByte,0,mFileByte.Length);
uploadtable.Visible = false;
successtable.Visible = true;
successinfo.InnerHtml = "<blockquote style=\"line-height:150%\">" + str_body + "</blockquote>";
saveFile.Close();
}
catch
{
myLabel.Text = "<font color=red>上传文件失败!请重新再试一次。</font>";
return;
}
}
//intRunTime = (int)(DateTime.Now - obj.dtStartTime).TotalMilliseconds;
}




















































后台:














































































































