javascript 和 上传文件的问题

<html>

<head>
<script language="javascript" >
var isOnly = "true"; //操作单个还是多个(多个则为数组)
var n=0; //初始化数组为0,之后随着化来变化,因为第一次不为数组,当第二次加一个File 时才变成数组,所以从1开始
var fileCount=1; //总共输入了多少个有值的控件File (暂时还没有用上)

var tempRow=0; //动态表格的临时行
var maxRows=0; //动态表格的临时列
var num = 1; //file 控件数组下标,从1开始,默认显示一个所以那个是 0
var fileCount=1; //整个操作中,总共用了多少个 File 控件

function operFileDiag()
{
var obj = new ActiveXObject("HMExtras.FileDlg");
obj.DisplayFileDlg();
}

function addFile()
{

var str = '<input type="file" size="50" name="uploadFile[';//待插入的文件控件
//var fstr = '<input type="file" value="" οnchange="txt.value=this.value" style="position:absolute;filter:alpha(opacity=0);" size="1" hidefocus name="uploadFile[';

//var buttonValue =' <input type="button" name="btnAddFile" value="添加 ……" size="30" id="button' + num + '" οnmοusemοve="f' + num + '.style.pixelLeft=event.x-60;f' + num + '.style.pixelTop=this.offsetTop;" οnclick="f' + num + '.click();addFile()"> '

var fileText;//得到文件控件的值
var ary;//分割文件,以'\'号
var fileTextValue;//取出最后的文件名
//alert("n = " + n + " num = " + num + " fileName = " + "uploadFile[" + n + "].file");
//alert(KmZszbfjForm.innerHTML);
fileText = document.all("uploadFile[" + n + "].file").value;
ary = fileText.split("\\");
fileTextValue = ary[ary.length-1];
if(fileText == "")
{
alert("请选择你所要上传的文件!");
return false;
}
document.all("uploadFile[" + n + "].file").style.display = "none";



//在前面一个 File 控件隐藏后,接着再在原来的位置上插入一个,则 File 变成数组了
str = str + num + '].file" ' + '/>';
//fstr = fstr + num + '].file" ' + 'id="f' + num + '"/>';
//alert("str = = " + str);
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
//document.getElementById('btnFile').insertAdjacentHTML("beforeEnd",buttonValue);
//document.getElementById("btnFile").removeChild(document.getElementById("button" + (num - 1)));


tempRow=fileTable.rows.length-1; //fileTable 就是那个动态的 table 的 ID 了
maxRows=tempRow;
tempRow=tempRow+1;
var Rows=fileTable.rows; //Rows 数组
var newRow=fileTable.insertRow(fileTable.rows.length); //插入新的一行
var Cells=newRow.cells; //Cells 数组
for (i=0;i<3;i++) //每行的2列数据,一列用来显示文件名,一列显示"删除"操作
{
var newCell=Rows(newRow.rowIndex).insertCell(Cells.length);
newCell.align="center";
switch (i)
{
case 0 : newCell.innerHTML="<td width='40%' align='left'><span id='"+n+"'></span></td>";break;
case 1 : newCell.innerHTML="<td width='20%' align='left'><a href='javascript:delTableFileRow(\"" +tempRow+ "\",\"" + n + "\")'>删除</a></TD>"; break;
case 2 : newCell.innerHTML="<td width='40%' align='left'>&nbsp;</TD>"; break;
}
}
maxRows+=1;
//alert("fileTextValue = " + fileTextValue);
document.getElementById(n).insertAdjacentText("beforeBegin",fileTextValue);
n++;
num++;
fileCount++;
alert(MyFile.innerHTML);
}

function delTableFileRow(rowNum,fileCount){
//if(fileTable.rows.length == 1)
//fileTable.deleteRow(0);
if (fileTable.rows.length >rowNum){
fileTable.deleteRow(rowNum);
}else
fileTable.deleteRow(fileTable.rows.length-1);
document.all("MyFile").removeChild(document.all("uploadFile[" + fileCount + "].file")); //从元素P上删除子结点File控件。(跟删除表格行同步)
fileCount--;
//if(n>0)n--; //else isOnly = "true";
}
</script>

</head>

<body>

<form id="kkk" action="/KmZszbfj.do" enctype="multipart/form-data"method="POST">
<table width="830" height="385" border="0" align="center"
cellpadding="0" cellspacing="0" id='tableA'>
<tr>
<td height="27" align="center" bgcolor="#E2F0FE" class="time">附件</td>
<td height="79" align="center" colSpan="3" bgcolor="#E2F0FE" class="time">

<div align="left">
<P id="MyFile">
<input type="file" size="50" name="uploadFile[0].file" value=""/>

</P>
<P id="btnFile">

<input type="button" name="btnAddFile" value="Add File" οnclick="addFile()"/>
</P>

<table width="100%" height="100%" border="0">
<tr><td width="40%" align="left"></td><td width="60%"></td></tr>
<tr>
<td colspan="2">
<table width="100%" border="0" id="fileTable" align="left"></table>
</td>
</table>
</div>
</td>

</tr>
</table>
</body></html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值