Ajax怎样做无刷新上传文件效果,方法是什么
Admin 2022-09-13 群英技术资讯 943 次浏览
这篇文章给大家分享的是“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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互的实例,帮助大家更好的理解和学习vue,感兴趣的朋友可以了解下
这段时间在开发一个腾讯文档全品类通用的 HTML 动态服务,为了方便各品类接入的生成与部署,也顺应上云的趋势,考虑使用 Docker 的方式来固定服务内容,统一进行制品版本的管理。本篇文章就将我在服务 Docker 化的过程中积累起来的优化经验分享出来,供大家参考。
这篇文章给大家分享的是用JS怎样求两个数最大值的方法。下文分享了三种方法,对新手学习JavaScript的使用有一定的帮助,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
图例组件展现了不同系列的标记(symbol),颜色和名字,可以通过点击图例控制哪些系列不显示,这篇文章主要给大家介绍了关于Echarts图例组件的相关资料,需要的朋友可以参考下
本篇文章给大家介绍一下javascript比较运算符(“===”和“==”)的匹配规则,以及if()条件的判断结果,希望对大家有所帮助!
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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