日期:2014-05-17 浏览次数:20725 次
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data"> <fieldset> <legend>HTML File Upload</legend> <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" /> <div> <label for="fileselect">Files to upload:</label> <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" /> <div id="filedrag">or drop files here</div> </div> <div id="submitbutton"> <button type="submit">Upload Files</button> </div> </fieldset> </form> <div id="messages"> Status Messages </div>
#filedrag { display: none; font-weight: bold; text-align: center; padding: 1em 0; margin: 1em 0; color: #555; border: 2px dashed #555; border-radius: 7px; cursor: default; } #filedrag.hover { color: #f00; border-color: #f00; border-style: solid; box-shadow: inset 0 3px 4px #888; }
// getElementById function $id(id) { return document.getElementById(id); } // // 输出信息 function Output(msg) { var m = $id("messages"); m.innerHTML = msg + m.innerHTML; }
// 判断当前浏览器中文件API是否可用 if (window.File && window.FileList && window.FileReader) { Init(); } // // 初始化程序 function Init() { var fileselect = $id("fileselect"), filedrag = $id("filedrag"), submitbutton = $id("submitbutton"); // 添加文件选择的事件监听 fileselect.addEventListener("change", FileSelectHandler, false); // 判断xmlhttprequest 2是否可用 var xhr = new XMLHttpRequest(); if (xhr.upload) { // file drop filedrag.addEventListener("dragover", FileDragHover, false); filedrag.addEventListener("dragleave", FileDragHover, false); filedrag.addEventListener("drop", FileSelectHandler, false); filedrag.style.display = "block"; // remove submit button submitbutton.style.display = "none"; } }