Ajax怎么实现上传图像功能

发布时间:2022-04-11 14:04:14 作者:iii
来源:亿速云 阅读:480

这篇“Ajax怎么实现上传图像功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Ajax怎么实现上传图像功能”文章吧。

最终效果展示

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" >
        <div class="progress-bar progress-bar-striped active"  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>

以上就是关于“Ajax怎么实现上传图像功能”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. 使用jquery Ajax实现上传附件功能
  2. JavaScript实现异步图像上传功能的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ajax

上一篇:Go1.18多模块Multi-Module工作区模式是什么

下一篇:vue+element-ui+axios多文件上传怎么实现并显示整体进度

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》