像google,QQ,163那样一次上传多个文件
前台页面
<form id="frmUpload" runat="server" enctype="multipart/form-data">
<div>
<table width="100%" border="0px" cellpadding="0px" cellspacing="2">
<tr height="80">
<td align="right" bgcolor="#EAEAEA">
上传图片:</td>
<td bgcolor="#f7f7f7" valign="top">
<table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
<tr height="30">
<td class="tab_font_left" align="left" width="100%">
<div>
<table width="100%" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
<tr id="trHeader" align="center">
<td width="60" height="25px;" align="center" bgcolor="#1C5E55" style="color: #FFFFFF">
序号</td>
<td bgcolor="#1C5E55" align="center" style="color: #FFFFFF">
上传文件</td>
</tr>
</table>
</div>
<div>
<input type='file' style="display: none;" size='50' id="file1" runat="server" />
<input type="button" name="Submit" value="添加上传文件" onclick="AddSignRow()" />
<input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
<input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
<br />
<span class="notetext">注:最多只能上传三个图片,如果需要更新,请先删除原图片</span>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr height="30">
<td align="center" colspan="2" bgcolor="#f7f7f7">
<asp:Button ID="btnInsert" Text="确认添加" runat="server" OnClick="btnInsert_Click" /> <asp:Button ID="btnBack" Text="放弃返回" runat="server" />
</td>
</tr>
</table>
</div>
</form>
<div>
<table width="100%" border="0px" cellpadding="0px" cellspacing="2">
<tr height="80">
<td align="right" bgcolor="#EAEAEA">
上传图片:</td>
<td bgcolor="#f7f7f7" valign="top">
<table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
<tr height="30">
<td class="tab_font_left" align="left" width="100%">
<div>
<table width="100%" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
<tr id="trHeader" align="center">
<td width="60" height="25px;" align="center" bgcolor="#1C5E55" style="color: #FFFFFF">
序号</td>
<td bgcolor="#1C5E55" align="center" style="color: #FFFFFF">
上传文件</td>
</tr>
</table>
</div>
<div>
<input type='file' style="display: none;" size='50' id="file1" runat="server" />
<input type="button" name="Submit" value="添加上传文件" onclick="AddSignRow()" />
<input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
<input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
<br />
<span class="notetext">注:最多只能上传三个图片,如果需要更新,请先删除原图片</span>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">
<tr height="30">
<td align="center" colspan="2" bgcolor="#f7f7f7">
<asp:Button ID="btnInsert" Text="确认添加" runat="server" OnClick="btnInsert_Click" /> <asp:Button ID="btnBack" Text="放弃返回" runat="server" />
</td>
</tr>
</table>
</div>
</form>
脚本:
<script type="text/javascript" language="javascript">
<!--
var TempCount=1;//不可删除!downmoon 20071025
// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
return foundObj;
}
function AddSignRow()
{
if(TempCount>3) return;
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
var newNameTD=newTR.insertCell(0);
newNameTD.innerHTML = newTR.rowIndex.toString();
var newNameTD=newTR.insertCell(1);
newNameTD.innerHTML = "<input name='MyFile" + rowID + "' id='MyFile" + rowID + "' type='file' size='50' />";
txtTRLastIndex.value = (rowID + 1).toString() ;
TempCount=TempCount+1;
}
function DeleteSignRow(rowid)
{
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
var rowIndex = signItem.rowIndex;
signFrame.deleteRow(rowIndex);
for(i=rowIndex;i<signFrame.rows.length;i++){
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
}
function ClearAllSign()
{
if(confirm('确定要清空所有上传的文件吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=rowscount - 1;i > 0; i--){
signFrame.deleteRow(i);
}
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
TempCount=1;
AddSignRow();
}
}
//-->
</script>
<!--
var TempCount=1;//不可删除!downmoon 20071025
// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
return foundObj;
}
function AddSignRow()
{
if(TempCount>3) return;
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
var newNameTD=newTR.insertCell(0);
newNameTD.innerHTML = newTR.rowIndex.toString();
var newNameTD=newTR.insertCell(1);
newNameTD.innerHTML = "<input name='MyFile" + rowID + "' id='MyFile" + rowID + "' type='file' size='50' />";
txtTRLastIndex.value = (rowID + 1).toString() ;
TempCount=TempCount+1;
}
function DeleteSignRow(rowid)
{
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
var rowIndex = signItem.rowIndex;
signFrame.deleteRow(rowIndex);
for(i=rowIndex;i<signFrame.rows.length;i++){
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
}
function ClearAllSign()
{
if(confirm('确定要清空所有上传的文件吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=rowscount - 1;i > 0; i--){
signFrame.deleteRow(i);
}
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
TempCount=1;
AddSignRow();
}
}
//-->
</script>
后台代码:
protected void btnInsert_Click(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 1; iFile < = Request .Files.Count; iFile++)
{ ///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile - 1];
if (postedFile ! = null && postedFile.ContentLength > 0)
{
//UploadFile
}
}
}
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 1; iFile < = Request .Files.Count; iFile++)
{ ///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile - 1];
if (postedFile ! = null && postedFile.ContentLength > 0)
{
//UploadFile
}
}
}