在以前的Web应用中,上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码如下:
\n
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default6.aspx.cs” Inherits=”Default6″ %>
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>多文件上传</title>
\n
<script language=”JavaScript” type=”text/javascript”>
function addFile()
{
var str = ‘<INPUT type=”file” size=”50″ NAME=”File”>’
document.getElementById(‘MyFile’).insertAdjacentHTML(“beforeEnd”,str)
}
</script>
\n
</head>
<body>
<form id=”form1″ runat=”server” enctype=”multipart/form-data”>
<div>
<center>
<h3>
多文件上传</h3>
<p id=”MyFile”>
<input type=”file” size=”50″ name=”File”></p>
<p>
<input type=”button” value=”增加(Add)” onclick=”addFile()”>
<input onclick=”this.form.reset()” type=”button” value=”重置(ReSet)”>
<asp:Button runat=”server” Text=”开始上传” ID=”UploadButton” onClick=”UploadButton_Click”></asp:Button>
</p>
<p>
<asp:Label ID=”strStatus” runat=”server” Font-Names=”宋体” Font-Bold=”True” Font-Size=”9pt”
Width=”500px” BorderStyle=”None” BorderColor=”White”></asp:Label>
</p>
</center>
</div>
</form>
</body>
</html>
\n
\n
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
\n
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
\n
private Boolean SaveImages()
{
/**////’遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
\n
/**//// ‘状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append(“上传的文件分别是:<hr color=red>”);
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
/**////’检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != “”)
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append(“上传的文件类型:” + postedFile.ContentType.ToString() + “<br>”);
strMsg.Append(“客户端文件地址:” + postedFile.FileName + “<br>”);
strMsg.Append(“上传文件的文件名:” + fileName + “<br>”);
strMsg.Append(“上传文件的扩展名:” + fileExtension + “<br><hr>”);
/**////’可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(“images/”) + fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
\n
protected void UploadButton_Click(object sender, EventArgs e)
{
SaveImages();
}
}
\n
在此再说一下insertAdjacentHTML
\n
参数有两个,第一个,插入位置;第二个,插入内容;
第一个参数的意义:
[插在这里,值为"beforeBegin"]<div id=test>[插在这里,值为"afterBegin"]
层内文字
[插在这里,值为"beforeEnd"]</div>[插在这里,值为"afterEnd"]
\n
另加一个类似功能的带删除功能的实例
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
<title>无标题页</title>
</head>
<body>
<input type=”button” onclick=”addline()” value=”增加一行”>
<table border=”1″ id=”test”>
<tr>
<td colspan=”2″><input type=”text” name=”tt[]“></td>
</tr>
</table>
<input type=”submit”>
</form>
<script>
function addline(content){
newline=document.all.test.insertRow();
newline.insertCell().innerHTML=’<input type=”text” name=”tt[]“><input type=”button” value=”删除此行” onclick=”del()”>’
}
function del(){
document.all.test.deleteRow(window.event.srcElement.parentElement.parentElement.rowIndex);
}
</script>
\n
</body>
</html>