Vue+SpirngBoot 实现单文件和多文件的上传

前端使用Vue,后端使用SpirngBoot,实现通过网页上传单个文件或者多个文件到后端服务器

一.单个文件上传

html部分

<input class="file" name="file" type="file" @change="update"/>

js部分

update(e){
   let file = e.target.files[0];           
   let param = new FormData(); //创建form对象
   param.append('file',file);//通过append向form对象添加数据     
   let config = {
      headers:{'Content-Type':'multipart/form-data'}
   };  //添加请求头
   this.$axios.post('/controller/uploadFile/single',param,config)
   .then(response=>{
        console.log(response.data);
   })        
}

java后端部分

@PostMapping("/controller/uploadFile/single")
public String uploadFileSingle(@RequestParam("file") MultipartFile file) {
    if(file!=null){
        System.out.println(file.length);
        return "ok";
    }else{
        return "fail";
    }    
}

二.多个文件上传

html部分

<input class="file" multiple name="file" type="file" @change="update"/>

js部分

update(e){
   let files = e.target.files;           
   let param = new FormData(); //创建form对象
   for(let i=0;i<files.length;i++){
       param.append('file',files[i]);//通过append向form对象添加数据     
   }
   let config = {
      headers:{'Content-Type':'multipart/form-data'}
   };  //添加请求头
   this.$axios.post('/controller/uploadFile/batch',param,config)
   .then(response=>{
        console.log(response.data);
   })        
}

java部分

@PostMapping("/controller/uploadFile/batch")
public String uploadFileBatch(@RequestParam("file") MultipartFile[] files) {
    if(files!=null){
        System.out.println(files.length);
        return "ok";
    }else{
        return "fail";
    }    
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值