1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <input type="file" id="ipt"> <button id="btn">按钮</button>
<div class="progress" style="width:500px; margin: 10px 0;"> <div class="progress-bar progress-bar-info progress-bar-striped active" id="percent" style="width:0%"> 0% </div> </div> <script src="lib/jquery.js"></script> <script> $('#btn').on('click', function () { var files = $('#ipt')[0].files; var fd = new FormData(); fd.append('file', files[0]); var xhr = new XMLHttpRequest(); xhr.open('post', 'http://www.liulongbin.top:3006/api/upload/avatar'); xhr.upload.onprogress = function (e) { if (e.lengthComputable) { var bili = (e.loaded / e.total * 100).toFixed(2) + '%'; $('#percent').css('width', bili).text(bili); } }; xhr.upload.onload = function () { $('#percent').removeClass().addClass('progress-bar progress-bar-success'); }; xhr.send(fd); }); </script>
|