【知识积累】表单异步提交(文件上传)

一、jquery.form.js

1、下载地址:

https://download.csdn.net/download/axin1240101543/10955452

2、使用

2.1、js代码

<script type="text/javascript" src="../js/jquery.form.js"></script>
<script type="text/javascript">
function sendMsg_file(){
            var msg = $("#msg_file").val();
            var msgFlag = checkContent(msg);
            if(msgFlag){
                if (confirm("确认发送?")) {
                    var options = {
                        url : 'telMassSendMsg.html',
                        type: 'post',
                        dataType: 'json',
                        success:function (data) {
                            if (data.code == 0) {
                                alert(data.msg);
                                window.location.reload();
                            } else {
                                alert(data.msg);
                            }
                        }
                    };
                    $("#telMassSendMsg_file").ajaxSubmit(options);
                }
            }
		}
</script>

2.2、html

<div id="portlet_tab2" class="tab-pane active">
						<div class="accordion">
							<form id="telMassSendMsg_file" action="telMassSendMsg.html" method="post" enctype="multipart/form-data" class="tab-pane">
								<table  style="margin:12px auto;width:90%;" class="table2">
									<tr>
										<th width="18%" class="f16 th-tel1" style="vertical-align: top;">上传联系人:</th>
										<td align="left">
											<a style="width: 15%;margin-bottom: 29px;" href="javascript:;" class="file"><span>未选择任何文件</span><input style="display: inline-block;float: left;" type="file" id="file1" name="file1" onchange="validate_img(this)" multiple></a>
										</td>
									</tr>
									<tr>
										<th width="18%" class="f16 th-tel2">短信内容:</th>
										<td align="left" >
											<div class="control-group success">
												<textarea rows="8"  class="input-td palette-night"  style="width: 80%; margin-bottom: 0px;margin-top: 40px;" id="msg_file" name="msg"></textarea>
											</div>
										</td>
									</tr>
									<tr>
										<td colspan="6"><button type="button" class="btn btn-primary" onclick="sendMsg_file()"><i class="icon-rss"></i> 发送</button></td>
									</tr>
								</table>
							</form>
						</div>
					</div>

2.3、web代码

    @RequestMapping(value="/telMassSendMsg")
    @ResponseBody
	public BaseVo telMassSendMsg(HttpServletRequest request, String tels, String msg){
        BaseVo baseVo = new BaseVo();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        MultiValueMap<String, MultipartFile> multiValueMap = multipartHttpServletRequest.getMultiFileMap();
        if (!multiValueMap.isEmpty() && StringUtils.isBlank(tels)){
            MultipartFile multipartFile = null;
            for (MultiValueMap.Entry<String, List<MultipartFile>> map: multiValueMap.entrySet()) {
                List<MultipartFile> files = map.getValue();
                multipartFile = files.get(0);
            }
            List<MsgVo> msgList = null;
            try {
                msgList = ExcelUtils.readExcel2Obj(MsgVo.class, 1, 2, multipartFile.getInputStream());
            } catch (IOException e) {
                log.error("读取Excel文件失败");
                baseVo.setCode(WebStatusEnums.FAILURE.getCode());
                baseVo.setMsg("读取Excel文件失败");
                return baseVo;
            }
            if (CollectionUtils.isNotEmpty(msgList)){
                StringBuilder sb = new StringBuilder();
                for (MsgVo m:msgList){
                    sb.append(m.getTelephone()).append(",");
                }
                tels = sb.toString().substring(0, sb.toString().length() - 1);
            }

        }
        if (StringUtils.isBlank(tels)){
            baseVo.setCode(WebStatusEnums.FAILURE.getCode());
            baseVo.setMsg("手机号不能为空");
            return baseVo;
        }
        //替换中文逗号,去除前后空格
        tels = tels.replace(",", ",").trim();
        String[] telArr = tels.split(",");
        for (String str:telArr){
            str = str.trim();
            boolean b = Pattern.compile(FORMAL).matcher(str).matches();
            if (!b){
                baseVo.setCode(WebStatusEnums.FAILURE.getCode());
                baseVo.setMsg(new StringBuilder("手机号:").append(str).append("不合法").toString());
                return baseVo;
            }
        }
        //返回0是成功
        ResultBean rb = customer.telSendMsg(telArr, msg);
        baseVo.setCode(rb.getCode());
        baseVo.setMsg(rb.getMsg());
        return baseVo;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值