1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>文件上传 原生ajax上传</title> 6 <style type="text/css"> 7 .container{ 8 width: 200px; 9 height: 20px; 10 background-color: gray; 11 } 12 #progress{ 13 height: 20px; 14 background-color: orange; 15 display: inline-block; 16 } 17 18 </style> 19 </head> 20 <body> 21 <form 22 enctype="multipart/form-data" method="post"> 23 上传文件1: <input type="file" name="firmware" id="firmware"><br /> 24 <div class='container'> 25 <span id="progress"></span> 26 </div> 27 </form> 28 <br> 29 <button onclick="fileSelected()">文件信息</button><button onclick="uploadFile()">确认上传</button> 30 <div id="info"> 31 <div id="fileName"></div> 32 <div id="fileSize"></div> 33 <div id="fileType"></div> 34 </div> 35 <div id="result"></div> 36 <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> 37 <script type="text/javascript"> 38 function fileSelected() { 39 var file = document.getElementById('firmware').files[0]; 40 if (file) { 41 var fileSize = 0; 42 if (file.size > 1024 * 1024) { 43 fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB'; 44 } else { 45 fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB'; 46 } 47 document.getElementById('fileName').innerHTML = 'Name: ' + file.name; 48 document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize; 49 document.getElementById('fileType').innerHTML = 'Type: ' + file.type; 50 } 51 } 52 53 function uploadFile() { 54 var fd = new FormData(); 55 fd.append("firmware", document.getElementById('firmware').files[0]); 56 var xhr = new XMLHttpRequest(); 57 xhr.upload.addEventListener("progress", uploadProgress, false); 58 xhr.addEventListener("load", uploadComplete, false); 59 xhr.addEventListener("error", uploadFailed, false); 60 xhr.addEventListener("abort", uploadCanceled, false); 61 xhr.open("POST", "/"); 62 xhr.send(fd); 63 } 64 65 function uploadProgress(evt) { 66 if (evt.lengthComputable) { 67 var percent = Math.round(evt.loaded * 100 / evt.total); 68 69 document.getElementById('progress').innerHTML = percent.toFixed(2) + '%'; 70 document.getElementById('progress').style.width = percent.toFixed(2) + '%'; 71 } 72 else { 73 document.getElementById('progress').innerHTML = 'unable to compute'; 74 } 75 } 76 77 function uploadComplete(evt) { 78 document.getElementById('result').innerHTML = evt.target.responseText; 79 } 80 81 function uploadFailed(evt) { 82 alert("There was an error attempting to upload the file."); 83 } 84 85 function uploadCanceled(evt) { 86 alert("The upload has been canceled by the user or the browser dropped the connection."); 87 } 88 89 </script> 90 91 </body> 92 </html>
可以直接转为uboot调用页面
jquery使用精简版: 别人改的...
<script type="text/javascript" src="https://leytton.gitee.io/simjq/releases/simJQ-2.2.min.js"> </script>
转载