【jquery ajax】实现文件上传提交

简介: 【jquery ajax】实现文件上传提交

JQuery实现文件上传提交

  定义UI结构

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
 <input type="file" id="file1">
    <button id="btnUpload">上传文件</button>
     <img src="" alt="" style="width: 200px;" id="img1">

  验证是否选择了文件

$('#btnUpload').on('click', function () {
            let files = $('#file1')[0].files;
            if (files.length <= 0) {
                return alert('请选择文件后在上传')
            }
        })

  向FormData中追加文件并发起ajax请求

//上传文件
            let fd = new FormData();
            fd.append('avator', files[0]);
            //发起jquery  ajax请求
                       $.ajax({
                method: 'post',
                url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                data: fd,
                processData: false,
                contentType: false,
                success: function (res) {
                    alert('上传成功')
                    $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url)
                    console.log(res.url);
                }
            })

  jquery实现loading效果

ajaxStart(callback)

Ajax请求开始时,执行ajaxStart函数,可以在ajaxStart的callback中显示loading效果。

自jqueyr版本1.8起,该方法只能被附加到文档,$(document).ajaxStart()函数会监听文档内所有ajax请求,当ajax请求开始会触发这个函数,ajax结束则会触发ajaxStop

<img src="./自媒体资源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px">
          $(document).ajaxStart(function () {
            $('#loading').show()
        })
        $(document).ajaxStop(function () {
            $('#loading').hide()
        })

  完整代码

<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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
</head>
<body>
    <input type="file" id="file1">
    <button id="btnUpload">上传文件</button>
    <br>
      <img src="" alt="" style="width: 200px;" id="img1">
    <img src="./自媒体资源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px">
    <script>
        //监听传输
        $(document).ajaxStart(function () {
            $('#loading').show()
        })
        $(document).ajaxStop(function () {
            $('#loading').hide()
        })
        //建立单击事件
        $('#btnUpload').on('click', function () {
            let files = $('#file1')[0].files;
            if (files.length <= 0) {
                return alert('请选择文件后在上传')
            }
            //上传文件
            let fd = new FormData();
            fd.append('avator', files[0]);
            //发起jquery  ajax请求
            $.ajax({
                method: 'post',
                url: 'http://www.liulongbin.top:3006/api/upload/avatar',
                data: fd,
                processData: false,
                contentType: false,
                success: function (res) {
                    alert('上传成功')
                    $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url)
                    console.log(res.url);
                }
            })
        })
    </script>
</body>

目录
相关文章
N..
|
25天前
|
XML JSON 前端开发
jQuery实现Ajax
jQuery实现Ajax
N..
16 1
|
3月前
|
开发框架 JSON JavaScript
ASP.NET Core3.1实战教程---基于Jquery单文件上传
ASP.NET Core3.1实战教程---基于Jquery单文件上传
26 0
|
2月前
|
JavaScript 前端开发 Java
jquery ajax+spring mvc上传文件
jquery ajax+spring mvc上传文件
|
3月前
|
前端开发 JavaScript
Jquery ajax捕获错误信息
Jquery ajax捕获错误信息
16 0
|
3月前
|
JSON 缓存 前端开发
Jquery中AJAX的应用
Jquery中AJAX的应用
41 0
|
4月前
|
JSON 前端开发 JavaScript
jQuery中ajax的使用
jQuery中ajax的使用
|
4月前
|
XML JavaScript 前端开发
【Web智能聊天客服】之JavaScript、jQuery、AJAX讲解及实例(超详细必看 附源码)
【Web智能聊天客服】之JavaScript、jQuery、AJAX讲解及实例(超详细必看 附源码)
48 0
|
5月前
|
XML 前端开发 JavaScript
什么是Ajax和jquery
什么是Ajax和jquery
38 0
|
4月前
|
JSON 前端开发 Java
利用Spring Boot处理JSON数据实战(包括jQuery,html,ajax)附源码 超详细
利用Spring Boot处理JSON数据实战(包括jQuery,html,ajax)附源码 超详细
57 0
|
4月前
|
敏捷开发 JavaScript 前端开发
❤❤❤【Vue.js最新版】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本❤❤❤
❤❤❤【Vue.js最新版】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本❤❤❤