<!-- 文件选择框 --> <input type="file" id="file1"> <!-- 上传按钮 --> <button id="btnUpload">上传</button> <img src="../images/loading.gif" alt="" style="display: none;" id="img"> <script> $(document).ajaxStart(function () { $('#img').show(); }) $(document).ajaxStop(function () { $('#img').hide(); }) $('#btnUpload').on('click', function () { // console.log('1'); var files = $('#file1')[0].files; console.log(files); if (files.length <= 0) { return alert('请选择文件后在上传!') } var fd = new FormData(); fd.append('avatar', files[0]) $.ajax({ type: 'POST', url: 'http://www.liulongbin.top:3006/api/upload/avatar', data: fd, contentType: false, processData: false, success(res) { console.log(res); } }) }) </script>
正文
使用jquery,FormData对象实现文件上传和loading效果
转载请注明出处:米耀华博客
还没有评论,来说两句吧...