ajax上传图片与预览图片demo

前端之家收集整理的这篇文章主要介绍了ajax上传图片与预览图片demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<!doctype html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport"
          content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
    <Meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ajax Upload demo</title>
</head>
<body>

<form id="uploadForm" enctype="multipart/form-data">
    <input id="file" type="file" name="file"/>
    <button id="upload" type="button">upload</button>
</form>
<img id="pre" src="#" width="800" height="600"/>
<button id="show" >show</button>
</body>
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.js"></script>
<script>
    $('#upload').click(upload);
    $('#file').change(function(){
                var reader = new FileReader();
        reader.readAsDataURL($('#file')[0].files[0]);
        reader.onload = function (e) {
            $('#pre').attr('src',this.result)
        }
    })
    function upload() {
        var form = new FormData();
        form.append("file",$('#file')[0].files[0]);
        form.append("name","jdlsajdlkas");
        form.append("order","1");

        var settings = {
            "async": true,"crossDomain": true,"url": "/attachments/","method": "POST","processData": false,"contentType": false,"mimeType": "multipart/form-data","data": form
        }

        $.ajax(settings).done(function (response) {
            console.log(response);
        });
    }
</script>
</html>
原文链接:https://www.f2er.com/ajax/161891.html

猜你在找的Ajax相关文章