日期:2014-05-17  浏览次数:20425 次

如何用JS生成多个FileUpload并在后台取得FileName值
如题 我在页面中 做了个点击事件 每次点击就生成一个FileUpload 该怎么写 在后台又该如何取得每个FileUpload的FileName值

------解决方案--------------------
Request.Files 循环
------解决方案--------------------
非常重要的一句话 <form id="form1" runat="server" enctype="multipart/form-data">


 HttpFileCollection files = HttpContext.Current.Request.Files; //遍历File表单元素

 for (int i = 0; i < files.Count; i++)
{
HttpPostedFile postedFile = files[i];
if (postedFile.ContentLength > 0)
{
string fileName = System.IO.Path.GetFileName(postedFile.FileName);

下面的应该懂了吧.
------解决方案--------------------
js是不能生成服务器端控件的,除非你用HTml表单

HTML code


    <script language="JavaScript" type="text/javascript">  
var index = 1;
function addFile(){
    var fartherID = "addedFile" + index;
    var divToAdd = document.createElement("div");
    divToAdd.id = fartherID;
    var str = '<INPUT style="width:284px;" type="file" contentEditable="false" id="subFile' + index + '" name="subFilename' + index + '" />&nbsp;&nbsp;' +
    '<a  fartherID="' +fartherID +'"  onclick="removeFileChild(this)" style="cursor: hand;">删除</a>';
    divToAdd.innerHTML = str;
    var fartherObject = document.getElementById('btAddFile');
    fartherObject.appendChild(divToAdd);
    index++;
}
    </script>

    <form id="form1" runat="server">

<div>
                                    <asp:FileUpload ID="SubFile0" runat="server" Width="284px" contenteditable="false" />
                                </div>
                                <div id="btAddFile">
                                </div>
                                <div>
                                    <a href="javascript:addFile()">添加附件</a>
                                </div>
</form>

------解决方案--------------------
HTML code

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jsDoc中文网-locale photo</title> 
<script> 
function AddMoreRow() 
{ 
    var oRow=event.srcElement.parentNode.parentNode; 
    var oTable=oRow.parentNode.parentNode; 
    oNewRow=oTable.insertRow(); 
    for(i=0;i<oRow.cells.length;i++) 
    { 
        oNewRow.insertCell().innerHTML=oRow.cells[i].innerHTML; 
    } 
    oRow.all("DelBtn").disabled=false; 
    oNewRow.all("DelBtn").disabled=false; 
} 
function DeleteMoreRow() 
{ 
    var oRow=event.srcElement.parentNode.parentNode; 
    var oTable=oRow.parentNode.parentNode; 
   if(oTable.rows.length>1) 
    { 
        oTable.deleteRow(oRow.rowIndex); 
        if(oTable.rows.length==1) 
        { 
           oTable.all("DelBtn").disabled=true; 
        } 
    } 
} 
function DisabledKeyInput() 
{ 
    if(event.keyCode!=8&&event.keyCode!=46) 
    { 
        event.returnValue=false; 
    } 
} 
</script> 
</head>

<body topmargin="10"> 
<form method="post" enctype="multipart/form-data"> 
<table><th>批量上传</th></table> 
<table>

<tr>

<td nowrap="nowrap">名称*</td> 
<td><input type