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实现上传图像功能的示例详解的文章就介绍到这了,更多相关ajax上传图像内容请搜索硕编程以前的文章或继续浏览下面的相关文章希望大家以后多多支持硕编程!

相关文章