记录一次ajax中使用formdata(文件和其他参数一起上传)

记录一次ajax中使用formdata(文件和其他参数一起上传)

问题来源

事情是这样的,在一个表单中有文件,也有其他文本等参数,后台让我用formData格式传输。疯狂百度后,有如下总结

具体解决方案

首先,除了formdata之外,要解决文件上传的问题。

1. 文件上传代码

1.form标签写法:

<form class="form-horizontal" style="margin-top: 20px;" id="inputer" enctype="multipart/form-data">

2.input标签写法

 <input type="file" id="fileExport" class='real-upload-btn' accept=".pdf">

注意点:
(1)form中加enctype=“multip加粗样式art/form-data”
(2)input框type类型为file

ps:注意,此处会有一个问题,可能有些同学也可能会遇到,input框type类型为file之后,框架的样式就全都没了,变成了很原始的样子,但是项目肯定不能那么丑哇,于是我是这样解决滴。
(1)写了一个a标签包在input之外,将a标签样式模拟成按钮

  .virtual-upload-btn {
        padding: 4px 10px;
        height: 20px;
        line-height: 20px;
        border: 1px solid#999;
        border-radius: 5px;
        text-decoration: none;
        color: #fff;
        position: relative;
        top: 7px;
        cursor: pointer;
    }

(2)令input的opacity:0
(3)让input定位在a标签上(子绝父相)

 .real-upload-btn {
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
    }

以上便可以自己模仿一个上传文件的按钮了。
(3)模仿上传后显示文件
在a标签后手动增加了一个span标签
布局如下:

<div class="col-md-8" style="top:50%;">
                                <a href="javascript:;" class="virtual-upload-btn">
                                    选择文件
                                    <input type="file" @change="upload" id="fileExport" class='real-upload-btn' accept=".pdf">
                                </a>
                                <span id="file-name" style="display: inline-block;position: absolute;padding: 6px 0px 6px 6px;width: 75%;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">{{fileName}}</span>
                            </div>

给按钮设置onchange事件,当有文件时,让文件名显示在按钮其后
js如下:

 upload(){
                let files = $('#fileExport').prop('files');
                if (files.length < 1) {
                    return layer.msg('请选择导入文件!');
                }else{
                    this.fileName=files[0].name
                }
            },

2.ajax代码

$.ajax({
                    async: false,
                    cache: false,
                    type: "POST",
                    data: formData,
                    dataType: 'JSON',
                    url: baseURL + '/plan-management-entity',
                    contentType: false, //必须
                    processData: false, //必须
                    success: function (result) {
                        // debugger
                        if (result.success) {
                            layer.msg(htl + '成功!');
                            _this.closeAddWin();
                            _this.getTableData();
                        } else {
                            layer.msg(htl + '失败!');
                            // console.log('失败');

                        }
                    },
                    error: function () {
                        layer.msg(htl + '失败!');
                        // console.log('错误');
                    }
                });
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值