Ajax怎样做无刷新上传文件效果,方法是什么
Admin 2022-09-13 群英技术资讯 920 次浏览
这篇文章给大家分享的是“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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了js实现购物网站放大镜功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
我们知道setup函数是组合API的核心入口函数,下面这篇文章主要给大家介绍了关于Vue3中SetUp函数的参数props、context的相关资料,需要的朋友可以参考下
这篇文章主要给大家介绍JavaScript数组操作filter、find、some、reduce的使用区别,对新手学习JavaScript数组操作有一定的帮助,下文有示例供大家参考,感兴趣的朋友就继续往下看吧。
如何用jQuery实现鼠标经过隐藏与显示的动画?有不少朋友对此感兴趣,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。
很多朋友比较喜欢玩一些网页小游戏,其实使用简单的JS代码也是可以实现有趣好玩的小游戏。这篇文章就给大家分享使用JS实现猜数字的游戏,实现代码及效果如下,感兴趣的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008