Home > Web Front-end > JS Tutorial > body text

Implement Ajax Submit to prompt the user when uploading files

php中世界最好的语言
Release: 2018-03-31 17:28:06
Original
1452 people have browsed it

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 ""; 
  }
Copy after login

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) { 
  } 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template