我想请问高手们,动态的添加上传控件,可以添加任意多个上传控件,并可以获取到每个上传文件的信息?
解决方案 »
- 新发布的网站在局域网中看不到
- 用JavaScript如何选中TreeView中的某一项?
- SQL问题 高手进!
- 哪里有可以安装[Community Server]论坛的虚拟主机?
- 问个编写自定义控件的问题(在线等?)
- 怎样实现在用户控件内点击搜索按钮时,在DataGrid中显示相应的数据?急症!!!
- win2003 server 的iis怎么不能使用asp页面
- 想了解一下~~.net程序员和java程序员现在哪个更吃香点~~~
- 看了微软Portal例程,返回一条记录用SqlDataReader,返回记录集用DataSet,为什么??
- 三层架构,调用webservice放哪一层
- 为什么我的更新无效?
- C#中,如何定义一个N行3列的,用于记录字符串的数组???
注意:页面上初始的file控件必须有runat="server"标志。也就是说,这个页面上必须至少有一个runat="server"的file控件,否则后台接收不到Request.Files。<FORM id="form1" runat="server">
<DIV id="div1">
<INPUT ID="File1" TYPE="file" NAME="File1" runat="server">
<INPUT TYPE="button" VALUE="添加附件" onclick="javascript:AddFile();">
<INPUT TYPE="button" VALUE="删除附件" onclick="javascript:RemoveFile();">
<ASP:LISTBOX id="ListBox1" Width="200px" Height="100px" runat="server"></ASP:LISTBOX>
<ASP:BUTTON id="Button1" runat="server" Text="保存" Width="60px"></ASP:BUTTON>
</DIV>
<ASP:LITERAL ID="lResult" Runat="server"></ASP:LITERAL>
</FORM>
<SCRIPT language="javascript">
<!--
function AddFile()
{
var file = document.getElementById("div1").firstChild;
if(file.value == "")
{
alert("请选择文件!");
return;
}
var ary = file.value.split("\");
var filename = ary[ary.length-1];
var bAddFile = true;
if(CheckOptionsExists(filename,document.getElementById("ListBox1")))
{
alert("文件已经存在列表中!");
div1.removeChild(file);
bAddFile = false;
}
var f = document.createElement("input");
f.type = "file";
f.name = "file"
div1.insertBefore(f,div1.firstChild);
if(!bAddFile)
{
return
}
var o = new Option();
o.innerText = filename;
o.value = file.uniqueID;
document.getElementById("ListBox1").appendChild(o);
file.style.display = "none";
}
function RemoveFile()
{
var lst = document.getElementById("ListBox1");
if(lst.selectedIndex == -1)
{
alert("请选择要删除的附件!");
return;
}
var id = lst.value;
div1.removeChild(document.all[id]);
lst.removeChild(lst.options[lst.selectedIndex]);
div1.firstChild.style.display = "";
}
//检查选项是否存在.
function CheckOptionsExists(value,ddl)
{
for(var i=0;i<ddl.options.length;i++)
{
if(ddl.options[i].innerText == value)
{
return true;
}
}
return false;
}
//-->
</SCRIPT>
后台代码就比较简单了。没有过多的处理,只是一个简单的保存。 private void Button1_Click(object sender, System.EventArgs e)
{
for(int i=0;i<Request.Files.Count;i++)
{
if(Request.Files[i].ContentLength >0)
{
string filename = System.IO.Path.GetFileName(Request.Files[i].FileName);
Request.Files[i].SaveAs(Server.MapPath(filename));
this.ListBox1.Items.Add(new ListItem(filename,filename));
} this.lResult.Text = "保存成功!";
}
}
var num = 0;
function cretaeelement()
{
num++;
document.all.addnum.value = num;
var a,b; a = document.createElement("<input ID="File1" TYPE="file" runat="server" NAME="File1"+ num + ">"); b = document.createElement("<br>");
document.all.div1.appendChild(a);
document.all.div1.appendChild(b);
}
}
</script>
代码有脚本错误?