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实现背景图片铺满整个屏幕(适配所有机型)

    vue实现背景图片铺满整个屏幕(适配所有机型)

    在网页设计中,背景全屏是一种常见的视觉效果,通过正确的CSS样式设置,可以实现背景全屏且内容在固定一屏大小内完全显示,如果内容超出一屏,则可以通过滚动条查看剩余内容,这种设计可以提升用户的浏览体验,使网页看起来更加整洁和专业
    2024-10-10
  • 多个Vue项目部署到服务器的步骤记录

    多个Vue项目部署到服务器的步骤记录

    这篇文章主要给大家介绍了关于多个Vue项目部署到服务器的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • vue组件实现列表自动无限循环的方法

    vue组件实现列表自动无限循环的方法

    最近刚好有个功能需要实现列表的无限循环滚动,这篇文章主要给大家介绍了关于vue组件实现列表自动无限循环的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • vue使用ArcGis API for js创建地图实现示例

    vue使用ArcGis API for js创建地图实现示例

    这篇文章主要为大家介绍了vue使用ArcGis API for js创建地图实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Vue CLI2升级至Vue CLI3的方法步骤

    Vue CLI2升级至Vue CLI3的方法步骤

    这篇文章主要介绍了Vue CLI2升级至Vue CLI3的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • vue实现列表无缝循环滚动

    vue实现列表无缝循环滚动

    这篇文章主要为大家详细介绍了vue实现列表无缝循环滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • 解决Vue中的生命周期beforeDestory不触发的问题

    解决Vue中的生命周期beforeDestory不触发的问题

    这篇文章主要介绍了解决Vue中的生命周期beforeDestory不触发的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • el-select 点击按钮滚动到选择框顶部的实现代码

    el-select 点击按钮滚动到选择框顶部的实现代码

    本文通过实例代码给大家分享el-select 点击按钮滚动到选择框顶部效果,主要代码是在visibleChange在这个popper里面找到.el-select-dropdown__list,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • vue axios 在页面切换时中断请求方法 ajax

    vue axios 在页面切换时中断请求方法 ajax

    下面小编就为大家分享一篇vue axios 在页面切换时中断请求方法 ajax,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • 一文详解Vue3的watch是如何实现监听的

    一文详解Vue3的watch是如何实现监听的

    watch这个API大家都很熟悉,今天这篇文章小编来带你搞清楚Vue3的watch是如何实现对响应式数据进行监听的,希望对大家有一定的帮助
    2024-11-11

最新评论