springMVC+ajax实现文件上传且带进度条实例

 更新时间:2017年01月18日 09:40:18   作者:肖哥哥  
本篇文章主要介绍了springMVC+ajax实现文件上传且带进度条实例,具有一定的参考价值,有兴趣的可以了解一下。

前端代码:

<form id= "uploadForm"> 
   <p >指定文件名: <input type="text" name="filename" value= ""/></p > 
   <p >上传文件: <input type="file" name="file"/></ p> 
   <input type="button" value="上传" onclick="doUpload()" /> 
</form> 

function doUpload() { 
   var formData = new FormData($( "#uploadForm" )[0]); 
   $.ajax({ 
     url: 'http://localhost:8080/xiaochangwei/file/upload' , 
     type: 'POST', 
     data: formData, 
     async: false, 
     cache: false, 
     contentType: false, 
     processData: false, 
     success: function (returndata) { 
       alert(returndata); 
     }, 
     error: function (returndata) { 
       alert(returndata); 
     } 
   }); 
}

后端:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
  public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, ModelMap model) {
    System.out.println("开始");
    String path = request.getSession().getServletContext().getRealPath("upload");
    String fileName = file.getOriginalFilename();
    // String fileName = new Date().getTime()+".jpg";
    System.out.println(path);
    File targetFile = new File(path, fileName);
    if (!targetFile.exists()) {
      targetFile.mkdirs();
    }

    // 保存
    try {
      file.transferTo(targetFile);
    } catch (Exception e) {
      e.printStackTrace();
    }
    model.addAttribute("fileUrl", request.getContextPath() + "/upload/" + fileName);
    return "result";
  }

如果前端有很多实体类数据同文件一同提交

可以修改后端方法为:

复制代码 代码如下:

upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, ModelMap model,User user)

利用下面的代码更可实现带有进度条的文件上传

<script type="text/javascript">

    function UpladFile() {
      var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
      var FileController = "http://localhost:8080/xiaochangwei/file/upload";          // 接收上传文件的后台地址 

      // FormData 对象
      var form = new FormData($( "#uploadForm" )[0]);

      // XMLHttpRequest 对象
      var xhr = new XMLHttpRequest();
      xhr.open("post", FileController, true);
      xhr.onload = function () {
        // alert("上传完成!");
      };

      xhr.upload.addEventListener("progress", progressFunction, false);
      xhr.send(form);
    }

    function progressFunction(evt) {
      var progressBar = document.getElementById("progressBar");
      var percentageDiv = document.getElementById("percentage");
      if (evt.lengthComputable) {
        progressBar.max = evt.total;
        progressBar.value = evt.loaded;
        percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
        if(evt.loaded==evt.total){
          alert("上传完成100%");
        }
      }
    } 

  </script>
  

  <progress id="progressBar" value="0" max="100"></progress>


<form id= "uploadForm">

  <input type="file" id="file" name="myfile" />
  <input type="button" onclick="UpladFile()" value="上传" />

</form>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis plus saveBatch方法方法执行慢导致接口发送慢解决分析

    mybatis plus saveBatch方法方法执行慢导致接口发送慢解决分析

    这篇文章主要为大家介绍了mybatis plus saveBatch方法导致接口发送慢解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • idea2019版Plugins中搜索不到任何插件的问题解决

    idea2019版Plugins中搜索不到任何插件的问题解决

    本文主要介绍了idea2019版Plugins中搜索不到任何插件的问题解决,插件搜不出来的主要原因是plugins.jetbrains.com ping不通,下面就来介绍一下解决方法,感兴趣的可以了解一下
    2023-09-09
  • springboot与vue实现简单的CURD过程详析

    springboot与vue实现简单的CURD过程详析

    这篇文章主要介绍了springboot与vue实现简单的CURD过程详析,围绕springboot与vue的相关资料展开实现CURD过程的过程介绍,需要的小伙伴可以参考一下
    2022-01-01
  • Spring Boot 文件上传原理解析

    Spring Boot 文件上传原理解析

    Spring Boot 文件上传原理其实就是Spring MVC,因为这部分工作是Spring MVC做的而不是Spring Boot,那么,SpringMVC又是怎么处理文件上传这个过程的呢?下面通过本文给大家详细介绍下,一起看看吧
    2018-03-03
  • Java中使用开源库JSoup解析HTML文件实例

    Java中使用开源库JSoup解析HTML文件实例

    这篇文章主要介绍了Java中使用开源库JSoup解析HTML文件实例,Jsoup是一个开源的Java库,它可以用于处理实际应用中的HTML,比如常见的HTML格式化就可以用它来实现,需要的朋友可以参考下
    2014-09-09
  • Windows下apache ant安装、环境变量配置教程

    Windows下apache ant安装、环境变量配置教程

    这篇文章主要介绍了Windows下apache ant安装、环境变量配置教程,ANT的安装很简单,本文同时讲解了验证安装是否成功的方法和使用方法实例,需要的朋友可以参考下
    2015-06-06
  • Java中的访问修饰符详细解析

    Java中的访问修饰符详细解析

    以下是对Java中的访问修饰符进行了详细的分析介绍,需要的朋友可以过来参考下
    2013-09-09
  • Java时间复杂度、空间复杂度的深入详解

    Java时间复杂度、空间复杂度的深入详解

    对于一个算法,其时间复杂度和空间复杂度往往是相互影响的,当追求一个较好的时间复杂度时,可能会使空间复杂度的性能变差,即可能导致占用较多的存储空间,这篇文章主要给大家介绍了关于Java时间复杂度、空间复杂度的相关资料,需要的朋友可以参考下
    2021-11-11
  • Java JUC中操作List安全类的集合案例

    Java JUC中操作List安全类的集合案例

    这篇文章主要介绍了JUC中操作List安全类的集合案例,本文罗列了不安全的集合和安全的集合进行对比,以及Java中提供的安全措施,需要的朋友可以参考下
    2021-07-07
  • DecimalFormat数字格式化用法详解

    DecimalFormat数字格式化用法详解

    这篇文章主要为大家详细介绍了DecimalFormat数字格式化用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03

最新评论