Ajax怎样做无刷新上传文件效果,方法是什么
Admin 2022-09-13 群英技术资讯 693 次浏览
这篇文章给大家分享的是“Ajax怎样做无刷新上传文件效果,方法是什么”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。最终效果展示

xhr发起请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" >
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<!--1、文件选择框 -->
<input type="file" id="file1">
<!-- 2、上传文件的按钮 -->
<button id="btnupload">上传文件</button>
<br/>
<div class="progress" style="width:300px;margin:5px;">
<div class="progress-bar progress-bar-striped active" style="width: 0%;" id="precent">
0%
</div>
</div>
<!-- 3、img标签 来显示上传成功以后的图片 -->
<img alt="" id="img" width="800">
<script>
var precent = document.querySelector('#precent')
var btnupload = document.querySelector('#btnupload')
btnupload.addEventListener('click', function() {
var files = document.querySelector('#file1').files
if (files.length <= 0) {
return alert('请选择要上传的文件')
}
var fd = new FormData()
fd.append('avatar', files[0])
var xhr = new XMLHttpRequest()
xhr.upload.onprogress = function(e) {
console.log(e);
if (e.lengthComputable) {
var h = Math.ceil((e.loaded / e.total) * 100)
precent.style.width = h + '%'
precent.innerHTML = h + '%'
console.log(h);
}
}
xhr.upload.onload = function() {
$('#precent').removeClass().addClass('progress-bar progress-bar-success')
}
xhr.open('post', 'http://www.liulongbin.top:3006/api/upload/avatar')
xhr.send(fd)
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = JSON.parse(xhr.responseText)
console.log(data);
if (data.status == 200) {
console.log('上传成功');
document.querySelector('#img').src = 'http://www.liulongbin.top:3006' + data.url
} else {
console.log('上传失败');
}
}
}
})
</script>
</body>
</html>
ajax发起请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.js"></script>
<style>
img {
width: 50px;
height: 50px;
display: none;
}
</style>
</head>
<body>
<input type="file" id="file1">
<button id="btnupload">上传文件</button>
</br>
<img src="5-121204193R5-50.gif" alt="" id="loading">
<script>
$(function() {
$(document).ajaxStart(function() {
$('#loading').show()
})
$(document).ajaxStop(function() {
$('#loading').hide()
})
$('#btnupload').on('click', function() {
var files = $('#file1')[0].files
if (files.length <= 0) {
alert('请选择文件')
}
console.log('ok');
var fd = new FormData()
fd.append('avatar', files[0])
$.ajax({
method: 'POST',
url: 'http://www.liulongbin.top:3006/api/upload/avatar',
data: fd,
processData: false,
contentType: false,
success: function(res) {
console.log(res);
}
})
})
})
</script>
</body>
</html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文主要介绍了react实现组件状态缓存的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要给大家分享怎样使用JS怎样实现数据监听的方法,下文实现步骤和讲解有一定的借鉴价值,感兴趣的朋友可以参考一下,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
本篇文章给大家带来了关于JavaScript的相关知识,其中主要为大家介绍了JavaScript 条件判断使用技巧详解,有需要的朋友可以借鉴参考下,下面一起来看一下,希望对大家有帮助。
vue全局水印怎么设置?对于创建水印相信大家应该都陌生,今天我们来了解以下vue全局水印的方法,下文有详细的实例供大家参考,感兴趣的朋友就跟随小编一起来学习一下吧。
方法:1、将需要求平均值的数存入数组中;2、使用“for(i=0;i<arr.length;i++){sum+=arr[i]}”语句遍历数组,计算多个数的总和并赋值给变量“sum”;3、使用“sum/arr.length”语句计算出平均值。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008