vue使用el-upload实现文件上传的实例代码

 更新时间:2024年01月03日 11:20:39   作者:数据大魔王  
这篇文章主要为大家详细介绍了vue使用el-upload实现文件上传,文中示例代码介绍的非常详细,对大家的学习或工作有一定的帮助,感兴趣的小伙伴们可以参考一下

引言

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

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

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

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

一、前台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

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使用el-upload实现文件上传的文章就介绍到这了,更多相关vue el-upload文件上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue2 如何实现div contenteditable=“true”(类似于v-model)的效果

    vue2 如何实现div contenteditable=“true”(类似于v-model)的效果

    这篇文章主要给大家介绍了利用vue2如何实现div contenteditable="true",就是类似于v-model的效果,文中给出了两种解决的思路,对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
    2017-02-02
  • vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例

    vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例

    今天小编就为大家分享一篇vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Vue3实现常见附件的预览功能

    Vue3实现常见附件的预览功能

    最近开发了一个建筑相关的移动端项目,其中有各种附件预览的功能,本文总结出了一些Vue常用的文件预览方式,希望对大家有一定的帮助
    2025-04-04
  • 关于Vue如何清除缓存的方法详解

    关于Vue如何清除缓存的方法详解

    缓存清除成为克服浏览器缓存中过时内容挑战的关键技术,术语“缓存清除”是指故意使静态资源失效或进行版本控制,迫使浏览器在发生更改时获取新资源,本文给大家介绍了Vue如何清除缓存,需要的朋友可以参考下
    2023-12-12
  • Vite和Vue CLI的优劣

    Vite和Vue CLI的优劣

    这篇文章主要介绍了Vite比Vue CLI快在哪里,帮助大家更好的理解和使用vue,感兴趣的朋友可以了解下
    2021-01-01
  • 谈谈对Vue Router的理解

    谈谈对Vue Router的理解

    vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用,这篇文章主要介绍了对Vue Router的理解,需要的朋友可以参考下
    2022-11-11
  • Vue axios和vue-axios的关系及使用区别

    Vue axios和vue-axios的关系及使用区别

    axios是基于promise的HTTP库,可以使用在浏览器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包装器,可以像插件一样安装使用:Vue.use(VueAxios, axios),本文给大家介绍Vue axios和vue-axios关系,感兴趣的朋友一起看看吧
    2022-08-08
  • ant-design-vue中tree增删改的操作方法

    ant-design-vue中tree增删改的操作方法

    这篇文章主要介绍了ant-design-vue中tree增删改的操作方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Vue.nextTick纯干货使用方法详解

    Vue.nextTick纯干货使用方法详解

    这篇文章主要为大家介绍了Vue.nextTick使用方法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Vue3+element实现表格数据导出

    Vue3+element实现表格数据导出

    这篇文章主要为大家学习介绍了Vue3如何利用element实现表格数据导出功能,文中的示例代码讲解详细,感兴趣的小伙伴快跟随小编一起学习一下吧
    2023-07-07

最新评论