1 Star 0 Fork 0

吴宇娜/my_nana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
02.使用jQuery实现文件上传.html 4.49 KB
一键复制 编辑 原始数据 按行查看 历史
吴宇娜 提交于 2021-05-15 22:52 . 提交消息
<!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="./lib/bootstrap-v4.6.0.css">
<style>
body {
padding: 10px;
}
.thumb {
display: block;
width: 500px;
height: 340px;
background-color: #efefef;
margin-top: 30px;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-4">
<div class="custom-file">
<!-- 文件选择框 -->
<input type="file" accept="image/png, image/jpeg, image/gif" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">Choose image</label>
</div>
</div>
<div class="col-sm-4">
<!-- 上传图片的按钮 -->
<button class=" btn btn-primary" id="btnUpload">上传图片</button>
<button class=" btn btn-danger" id="btnPOST">测试POST请求</button>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<!-- 上传的进度条 -->
<div class="progress mt-3">
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="25" aria-valuemin="0"
aria-valuemax="100">0%</div>
</div>
<!-- loading 图标 -->
<div class="spinner-border text-primary loading" style="display: none;" role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<figure class="figure">
<!-- 缩略图 -->
<img src="..." class="thumb figure-img img-fluid rounded" alt="已上传的图片">
<figcaption class="figure-caption">已上传的图片</figcaption>
</figure>
</div>
</div>
</div>
<!-- 导入 jQuery -->
<script src="./lib/jquery-v3.6.0.js"></script>
<script>
$(function () {
// 为 document 绑定 ajaxStart 和 ajaxStop 事件
$(document).ajaxStart(function () {
console.log('当前页面发起了一个 Ajax 请求!')
// 展示 loading 效果
$('.loading').show()
})
$(document).ajaxStop(function () {
console.log('当前页面的 Ajax 请求完成了!')
// 隐藏 loading 效果
$('.loading').hide()
})
// 封装 jQuery 上传文件的方法
function uploadImage(fd) {
$.ajax({
type: 'POST',
url: 'http://www.liulongbin.top:3006/api/upload/avatar',
data: fd,
// contentType: false 用来指定请求体的编码格式为 multipart/form-data
contentType: false,
// processData: false 表示不需要对请求体中的数据进行 URL 的编码,而是直接把 FormData 发送给服务器
processData: false,
// 通过 xhr 这个函数,返回一个自定义的 xhr 对象,给 $.ajax 使用
xhr: function () {
const xhr = new XMLHttpRequest()
// 监听上传进度的事件
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
// 动态计算上传的百分比
const percent = Math.ceil(e.loaded / e.total * 100)
// 操作 DOM(链式编程)
$('.progress-bar').html(`${percent}%`).css('width', `${percent}%`)
}
})
return xhr
},
success: function (res) {
if (res.status === 200) {
$('.thumb').attr('src', 'http://www.liulongbin.top:3006' + res.url)
} else {
alert(res.message)
}
}
})
}
// 为上传按钮绑定点击事件处理函数
$('#btnUpload').on('click', function () {
// 拿到用户选择的文件们
const files = $('#customFile')[0].files
if (files.length === 0) return alert('请选择要上传的图片!')
// 创建 FormData 对象
const fd = new FormData()
fd.append('avatar', files[0])
// 调用上传文件的方法
uploadImage(fd)
})
// 点击按钮,发起 POST 测试的请求
$('#btnPOST').on('click', function () {
$.post('http://www.liulongbin.top:3006/api/post', { name: 'zs', age: 20 }, function (res) {
console.log(res)
})
})
})
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wu-yuna/my_nana.git
git@gitee.com:wu-yuna/my_nana.git
wu-yuna
my_nana
my_nana
master

搜索帮助