溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用JS進(jìn)行目錄上傳(相當(dāng)于批量上傳)

發(fā)布時(shí)間:2020-07-13 12:44:03 來(lái)源:網(wǎng)絡(luò) 閱讀:399 作者:yanyan507 欄目:web開(kāi)發(fā)

腳本使用了WScript.Shell和Scripting.FileSystemObject的組件,所以必須要在IE下面和打開(kāi)安全選項(xiàng)中運(yùn)行; 另外還用到了Jquery. 代碼只是客戶(hù)端代碼, 至于服務(wù)器的接收代碼網(wǎng)上好多了  <!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=gb2312" /> <title>測(cè)試文件上傳</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script language="javascript" type="text/javascript"> $(function(){ //ShowFolderFileList("D:\\CodeNet\\web\\Images\\shopTemplets"); }); function ShowFolderFileList(FilePath){ var fso, f, fc, sf; fso = new ActiveXObject("Scripting.FileSystemObject"); try{ f = fso.GetFolder(FilePath); }catch(err){ alert("文件路徑錯(cuò)誤或者不存在!!"); return false; } // 列出所有文件 fc = new Enumerator(f.files); var fileName = ""; for(;!fc.atEnd();fc.moveNext()){ fileName=fc.item().Name; $('<div filePath="' + FilePath + fileName + '" uploadState="wait">' + FilePath + fileName + '</div>').appendTo('#showArea'); } // 循環(huán) 遞歸 讀取 文件夾的文件 sf = new Enumerator(f.SubFolders); var folderName = ""; for(;!sf.atEnd();sf.moveNext()){ folderName = sf.item().Name; ShowFolderFileList(FilePath + folderName + "/"); } } function startUpload(){ var s = $('#uploaddir').val().replace( /\\/gi, '/' ) ; if(s.substring(s.length - 1 , s.length) != '/'){ s += '/' } ShowFolderFileList(s) uploadFile(); } // 上傳 function uploadFile(){ if($('#showArea div[uploadState=wait]').length > 0){ var thisNode = $('#showArea div[uploadState=wait]').eq(0) var WshShell=new ActiveXObject("WScript.Shell"); $('#fileupload').focus(); WshShell.SendKeys($(thisNode).attr('filePath')); // 路徑中不有是中文 uploadForm.submit(); $('#fileupload').focus(); $('#fileupload').get(0).createTextRange().select(); WshShell.SendKeys('{del}'); var dotStr = '.'; $('<span></span>').appendTo(thisNode).css('color', 'green'); var uploadState = setInterval(function(){ if($(thisNode).attr('uploadState') == 'ok'){ clearInterval(uploadState); $(thisNode).find('span').css('color', 'red').text('(完成)'); uploadFile(); }else{ if(dotStr.length > 15){ dotStr = '.'; }else{ dotStr += '.'; } $(thisNode).find('span').text('(上傳中' + dotStr + ')'); } }, 1000); } } // 在iframe 的返回頁(yè)面中調(diào)用此函數(shù) 即可實(shí)現(xiàn)循環(huán)上傳,,否則為死循環(huán) function uploadFinish(){ $('#showArea div[uploadState=wait]').eq(0).attr('uploadState', 'ok'); } </script> <style type="text/css"> body,td,th { font-family: "微軟雅黑", Tahoma, Helvetica, Arial, \5b8b\4f53, sans-serif; } </style> </head> <body> <form action="/a.html" method="post" name="loginForm" > <input name="uploaddir" id="uploaddir" type="text" value="D:\a" /> <input type="button" value="開(kāi)始" id="startIt" name="startIt" </form> <form action="/index/upload" method="post" name="uploadForm" enctype="multipart/form-data" target="hidden_frame"> <input type="file" name="fileupload" id="fileupload" /> <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe> </form> </iframe> <div id="showArea"> </div> </body> </html>

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI