页面:
<div class="container"> <label>请输入标题:</label> <input type="text" placeholder="请输入" id="title" autocomplete="off"> <label>请输入您的昵称:</label> <input type="text" placeholder="请输入" id="name" autocomplete="off"> <label>附件:</label> <input id="file" type="file" value="" placeholder="附件"> <input id="send" type="button" value="提交"> </div>
JS:
$("#send").click(function () { //创建表单 var formData = new FormData(); var title = $("#title").val(); var name = $("#name").val(); var file = $("#file")[0].files[0]; //填充数据 formData.append("title",title); formData.append("name",name); formData.append("file",file); $.ajax({ type: "POST", url: "/mailtool/mail/send", //data: msg="+msg+"&title="+title+"&name="+name+"&file="+file, data: formData, contentType: false, processData: false, mimeType:"multipart/form-data", success: function(data){ if(data == "success"){ alert("发送成功"); }else{ alert(data); } } }); })
后台:
@RestController @RequestMapping("/mail") public class Send { @PostMapping(value = "/send",produces="text/html;charset=utf-8") @ResponseBody public String send(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam("title") String title,@RequestParam("name") String name){
//todo
} }