vue使用el-upload实现文件上传功能

 更新时间:2022年04月21日 16:34:37   作者:浔者  
这篇文章主要为大家详细介绍了vue使用el-upload实现文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

因为我是vue+springboot前后分离,要跨域,就不能用默认的action写请求地址,我用axios时最困扰的就是怎么拿到那个真实的文件,然后给传给后台。

其实可以通过自带的onchanne触发方法获得文件列表,文件信息中那个raw就是真实的文件。

写的时候,刚开始我是直接把el-upload里面的button中加了点击事件,但是每次文件还没选,就已经向后台发出请求了,当然传不过去,于是外面套了个form。

element-ui组件使用可以查看文档

upload.vue

<template>
    <el-form>
       
        <el-form-item label="姓名" prop="name">
            <el-input v-model="name"></el-input>
        </el-form-item>
       
        <el-form-item>
            <el-upload ref="upfile"
                style="display: inline"
                :auto-upload="false"
                :on-change="handleChange"
                :file-list="fileList"
                action="#">
                <el-button  type="success">选择文件</el-button>
            </el-upload>
        </el-form-item>
        <el-form-item>
            <el-button  type="success" @click="upload">点击上传</el-button>
        </el-form-item>
    </el-form>
    
</template>
<script>
export default {
    name:'UploadUi',
    data(){
        return{
            name:'',
            fileList:[]
        }
    },
    methods:{
        //通过onchanne触发方法获得文件列表
         handleChange(file, fileList) {
            this.fileList = fileList;
            console.log(fileList)
        },
        upload(){
            
            let fd = new FormData();
            fd.append("name",this.name);
            this.fileList.forEach(item=>{
                //文件信息中raw才是真的文件
                fd.append("files",item.raw);
                //console.log(item.raw)
            })
            this.$axios.post('/uploadUi',fd).then(res=>{
                if (res.data.code === 200) {
                    //console.log(res);
                    this.$message('上传成功')
                }else{
                    this.$message('失败')
                }
            })
        },
    }
}
</script>

springboot后台 uploadController.java

package com.example.demo.controller;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
 
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import com.example.demo.entity.ListParam;
import com.example.demo.entity.MyUser;
import com.example.demo.entity.Result;
 
@RestController
@ResponseBody
@CrossOrigin
@RequestMapping("/api")
public class UploadController {
    
    @PostMapping("/uploadUi")
    public Result upFile(@RequestParam("name")String name,@RequestParam("files") MultipartFile[] files ) {
         System.out.println("开始");
         System.out.println(name);
         if(files != null) {
             for(MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                System.out.println(fileName);
                try{
                    File mkdir = new File("F:\\app\\file");
                    if(!mkdir.exists()) {
                        mkdir.mkdirs();
                    }
                    //定义输出流,将文件写入硬盘
                     FileOutputStream os = new FileOutputStream(mkdir.getPath()+"\\"+fileName);
                      InputStream in = file.getInputStream();
                      int b = 0;
                      while((b=in.read())!=-1){ //读取文件 
                        os.write(b);
                      }
                      os.flush(); //关闭流 
                      in.close();
                      os.close();
                      
                }catch(Exception  e) {
                    e.printStackTrace();
                    return new Result(401,"失败");
                }
            }
             return new Result(200,"成功");
         }else {
             return new Result(401,"文件找不到");
         }
        
    }
    
}

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

相关文章

  • vue openlayers实现台风轨迹示例详解

    vue openlayers实现台风轨迹示例详解

    这篇文章主要为大家介绍了vue openlayers实现台风轨迹示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Vue-cli项目部署到Nginx服务器的方法

    Vue-cli项目部署到Nginx服务器的方法

    这篇文章主要介绍了Vue-cli项目部署到Nginx服务器的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • vue实现点击当前标签高亮效果【推荐】

    vue实现点击当前标签高亮效果【推荐】

    这篇文章主要介绍了vue实现点击当前标签高亮效果的思路详解,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-06-06
  • vue动态加载SVG文件并修改节点数据的操作代码

    vue动态加载SVG文件并修改节点数据的操作代码

    这篇文章主要介绍了vue动态加载SVG文件并修改节点数据的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • React Diff算法不采用Vue的双端对比原因详解

    React Diff算法不采用Vue的双端对比原因详解

    这篇文章主要介绍了React Diff算法不采用Vue双端对比算法原因详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • vue项目中扫码支付的实现示例(附demo)

    vue项目中扫码支付的实现示例(附demo)

    本文主要介绍了vue项目中扫码支付的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Vue动态路由路径重复及刷新丢失页面问题的解决

    Vue动态路由路径重复及刷新丢失页面问题的解决

    这篇文章主要介绍了Vue动态路由路径重复及刷新丢失页面问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • vue.js diff算法原理详细解析

    vue.js diff算法原理详细解析

    这篇文章主要介绍了vue.js diff算法原理详细解析,diff算法可以看作是一种对比算法,对比的对象是新旧虚拟Dom。顾名思义,diff算法可以找到新旧虚拟Dom之间的差异,但diff算法中其实并不是只有对比虚拟Dom,还有根据对比后的结果更新真实Dom
    2022-06-06
  • Vue2.0 http请求以及loading展示实例

    Vue2.0 http请求以及loading展示实例

    下面小编就为大家分享一篇Vue2.0 http请求以及loading展示实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • Vue ElementUI table实现表格斜线分隔线

    Vue ElementUI table实现表格斜线分隔线

    这篇文章主要为大家详细介绍了Vue ElementUI table实现表格斜线分隔线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论