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">
    <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>

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一夕ξ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值