This time I will bring you the implementation of Ajax Submit to prompt the user when uploading files. Ajax Submit prompts the user when uploading files. What are the precautions? . Here is a practical case. Let’s take a look.
springmvc backend:
@RequestMapping(value="scoreFileUpload",produces = "text/html; charset=utf-8") @ResponseBody public String upload(HttpSession session,@RequestParam("file1") MultipartFile file,@RequestParam("paperId") String paperId,HttpServletRequest request) { //获取登录人员的id Integer userId = (Integer) session.getAttribute(BaseConstant.SESSION_UERID_KEY); JSONObject json = new JSONObject(); if (request instanceof MultipartHttpServletRequest) { //获取上传文件的文件名 String fileName = file.getOriginalFilename(); String subfix = FileUtils.getFileExtend(fileName); } return ""; }
js: end
$('#fileForm').submit(function() { var _businessDetailId = $("#businessDetailId").val(); var _paperId = $("#paperId").val(); var url = "scoreFileUpload?paperId="+_paperId+"&businessDetailId="+_businessDetailId; var optionss = { dataType:"text/html", type:'post', url: url, // beforeSubmit:showRequest, complete:showResponse, clearForm:false, timeout: 3000000 }; //提交表单 $(this).ajaxSubmit(optionss); // !!! Important !!! // 为了防止普通浏览器进行表单提交和产生页面导航(防止页面刷新?)返回false return false; });<pre name="code" class="javascript">function showResponse(data) { data = JSON.parse(data.responseText); if(data.type != null) { } }
If the returned type is json, this problem only It has happened under IE, but it is normal in Chrome and Firefox. To solve the problem, only text/html can be returned.
The type returned by the back end cannot be json, but text/html, and then convert the returned type to json type.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Ajax+Spring to implement file upload
How to use Ajax to dynamically load data
The above is the detailed content of Implement Ajax Submit to prompt the user when uploading files. For more information, please follow other related articles on the PHP Chinese website!