方式一:事先寫好多個(gè)input.在點(diǎn)擊時(shí)才顯示。也就是說上傳的最大個(gè)數(shù)是寫死了的。
html
<p>
<a href='#' onclick='javascript:viewnone(more1)'> 添加附件 </a>
<div id='more1' style='display:none'>
<input type="file" name="attach1" size="50"javascript:viewnone(more2)>
</span>
</div>
<div id='more2' style='display:none'>
<input type="file" name="attach2" size="50"'>
</div>
</p>
js
<SCRIPT language="javascript">
function viewnone(e){
e.style.display=(e.style.display=="none")?"":"none";
}
</script>
方式二:這種方式的動(dòng)態(tài)多文件上傳是實(shí)現(xiàn)了的,很簡單的,不說廢話看code
html
<input type="button" name="button" value="添加附件" onclick="addInput()">
<input type="button" name="button" value="刪除附件" onclick="deleteInput()">
<span id="upload"></span>
js
<script type="text/javascript">
var attachname = "attach";
var i=1;
function addInput(){
if(i>0){
var attach = attachname + i ;
if(createInput(attach))
i=i+1;
}
}
function deleteInput(){
if(i>1){
i=i-1;
if(!removeInput())
i=i+1;
}
}
function createInput(nm){
var aElement=document.createElement("input");
aElement.name=nm;
aElement.id=nm;
aElement.type="file";
aElement.size="50";
//aElement.value="thanks";
//aElement.onclick=Function("asdf()");
if(document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
}
function removeInput(nm){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null)
return false;
return true;
}
</script>
方式三:動(dòng)態(tài)多文件上傳,只是在oFileInput.click();這個(gè)地方,這樣做就不能上傳這個(gè)文件了,因?yàn)榘l(fā)現(xiàn)它在上傳之時(shí)就把這個(gè)input中的文件置空了。很可能是為了安全著想吧!
另外還有一點(diǎn)就是說,click()只有在ie中才能正常運(yùn)行。
雖說這種方式最終沒能實(shí)現(xiàn)上傳,但還是留下來參考,看看是否有人可以真正實(shí)現(xiàn)上傳。
html
<A href="javascript:newUpload();">添加附件</A>
<TABLE width="100%" border="0" cellpadding="0" cellspacing="1">
<TBODY id="fileList"></TBODY>
</TABLE><DIV id="uploadFiles" style="display:block"></DIV>
js
<SCRIPT language="javascript">
//---新建上傳
function newUpload(){
var oFileList = document.getElementById("fileList");
var fileCount = oFileList.childNodes.length + 1;
var oFileInput = newFileInput("upfile_" + fileCount);
if(selectFile(oFileInput)){
addFile(oFileInput);
}
}
//----選擇文件
function selectFile(oFileInput){
var oUploadFiles = document.getElementById("uploadFiles");
oUploadFiles.appendChild(oFileInput);
oFileInput.focus();
oFileInput.click();//不能這樣做,可能是為了安全著想吧!
var fileValue = oFileInput.value;
if(fileValue == ""){
oUploadFiles.removeChild(oFileInput);
return false;
}
else
return true;
}
//---新建一個(gè)文件顯示列表
function addFile(oFileInput){
var oFileList = document.getElementById("fileList");
var fileIndex = oFileList.childNodes.length + 1;
var oTR = document.createElement("TR");
var oTD1 = document.createElement("TD");
var oTD2 = document.createElement("TD");
oTR.setAttribute("id","file_" + fileIndex);
oTR.setAttribute("bgcolor","#FFFFFF");
oTD1.setAttribute("width","6%");
oTD2.setAttribute("width","94%");
oTD2.setAttribute("align","left");
oTD2.innerText = oFileInput.value;
oTD1.innerHTML = '<A href="javascript:removeFile('+ fileIndex + ');">刪除</A>';
oTR.appendChild(oTD1);
oTR.appendChild(oTD2);
oFileList.appendChild(oTR);
}
//---移除上傳的文件
function removeFile(fileIndex){
var oFileInput = document.getElementById("upfile_" + fileIndex);
var oTR = document.getElementById("file_" + fileIndex);
uploadFiles.removeChild(oFileInput);
fileList.removeChild(oTR);
}
//---創(chuàng)建一個(gè)file input對(duì)象并返回
function newFileInput(_name){
var oFileInput = document.createElement("INPUT");
oFileInput.type = "file";
oFileInput.id = _name;
oFileInput.name = _name;
oFileInput.size="50";
//oFileInput.setAttribute("id",_name);
//oFileInput.setAttribute("name",_name);
//oFileInput.outerHTML = '<INPUT type=file id=' + _name + ' name=' + _name + '>';
//alert(oFileInput.outerHTML);
return oFileInput;
}
</SCRIPT>
posted on 2007-01-26 17:21 重歸本壘(BNBN) 閱讀(1656) 評(píng)論(4) 編輯 收藏 引用 所屬分類: JS
呵呵,我的方法不知道和你的三種方法有沒有可比性,個(gè)人感覺還不錯(cuò)! @施偉 施偉 的做法,是不是還是不能解決,先選擇了一個(gè)文件,提交服務(wù)器之后這個(gè)file input域的值又被自動(dòng)清空的問題? 回復(fù) 更多評(píng)論 第二種方法不錯(cuò) 回復(fù) 更多評(píng)論 評(píng)論
# re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-01-27 13:20 施偉
做一個(gè) 添加附件 然后做一個(gè)type為file的input框,把此框和span定位重疊起來 把file框透明度設(shè)置為0 即完全看不到,但是確實(shí)存在。這個(gè)時(shí)候點(diǎn)span的時(shí)候就是在點(diǎn)這個(gè)file框 但是看不到file框子 是不是實(shí)現(xiàn)了呢? 然后再結(jié)合你第二種的方式給框編號(hào) 動(dòng)態(tài)增加就可以實(shí)現(xiàn)多文件上傳了 。
呵呵 我在我的程序里面這樣實(shí)現(xiàn)的 很好用 如果有興趣討論到我blog留言 或者發(fā)郵件給我吧 多交流。。。
回復(fù) 更多評(píng)論 # re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-01-29 18:00 重歸本壘(BNBN)
呵呵!施偉,你這樣做,如果實(shí)現(xiàn)了,那么比我的方法更勝一籌了,我以前也這樣考慮過,只是覺的好麻煩,而沒有去實(shí)現(xiàn)它!
另外,還非常謝謝你能關(guān)注我的Bolg!
回復(fù) 更多評(píng)論 # re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-02-12 17:12 路過的
# re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-06-04 11:28 sangern