ajax文件上传

 


<!-- Modal -->
<div class="modal fade" id="myModal" style="margin-top: 10%" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">图片上传</h4>
            </div>
            <div class="modal-body" id="modal-body">
                <form method="post" id="img_upload" action="${request.contextPath}/slideshow/fileUpload" enctype= "multipart/form-data">
                    <div class="form-group">
                        <label for="weight">权重</label>
                        <input type="text" class="form-control" id="weight"  name="weight" placeholder="请输入数字" required onkeyup="this.value=this.value.replace(/\D/g,'')">
                    </div>
                    <div class="form-group">
                        <label for="title">标题</label>
                        <input type="text" class="form-control" id="title" placeholder="请输入标题">
                    </div>
                    <div class="form-group">
                        <label for="file">图片上传</label>
                        <input type="file" name="file" required id="file">
                        <p class="help-block">请选择图片</p>
                    </div>
                    <span id="btn_upload" class="btn btn-primary">上传</span>
                </form>
            </div>
        </div>
    </div>
</div>
$(function () {
        $("#btn_upload").click(function () {


            let title = $("#title").val();
            let file = $('#file')[0].files[0];
            let weight = $("#weight").val();

            let formData = new FormData();

            formData.append("file",file);
            formData.append("title", title);
            formData.append("weight", weight);

            $.ajax({
                url: "${request.contextPath}/slideshow/fileUpload",
                dataType:'json',
                type:'POST',
                async: false,
                data: formData,
                processData : false, // 使数据不做处理
                contentType : false, // 不要设置Content-Type请求头
                success:function(data){
                    if(data.result === 200){
                        //
                    }else if(data.result === 201) {
                        //
                    }else if( data.result === 220 ){
                        //
                    } else {
                        //
                    }
                },
                error:function(response){
                    console.log(response);
                }
            });
        });
    });

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值