您现在的位置是:群英 > 开发技术 > web开发
用Ajax怎样实现上传图像,代码如何写
Admin发表于 2022-07-27 17:57:21553 次浏览
这篇文章分享给大家的内容是关于用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>

通过以上内容的阐述,相信大家对“用Ajax怎样实现上传图像,代码如何写”已经有了进一步的了解,更多相关的问题,欢迎关注群英网络或到群英官网咨询客服。

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

标签: Ajax上传图像
相关信息推荐
2022-11-02 18:18:36 
摘要:在编写C语言源代码时,应该多使用注释,这样有助于对代码的理解。在C语言中有两种注释方式:
2022-05-11 11:32:29 
摘要:这篇文章主要介绍了Python装饰器,装饰器Decorator从字面上理解,就是装饰对象的器件,其的特点是特点是函数是作为其参数出现的,装饰器还拥有闭包的特点,下面来看看文中的具体内容
2022-01-17 18:41:54 
摘要:这篇文章我们来了解Java中enum的用法,enum的用法还是比较多的,下文介绍了很多enum的使用且有实录供大家参考,有需要的朋友可以参考了解看看,接下来就跟随小编来一起学习一下吧!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部