vue+elementUI 文件上传功能(表单)

这篇博客介绍了如何在Vue.js中使用Element UI组件库进行文件上传,包括普通文件上传和与表单结合的上传方式。在普通文件上传示例中,展示了更新歌手头像的实现,而表单结合的文件上传则演示了添加歌曲信息的过程,涉及到上传歌曲文件并保存到数据库。
摘要由CSDN通过智能技术生成

elementUI普通的文件上传比较简单,但是当与表单结合是就会比较复杂。下面分别是普通文件更新和与表单相互结合的文件上传功能实现。

           //普通的文件上传
           <template slot-scope="scope">
              <div class="singer-img">
                <img :src="getUrl(scope.row.picture)" width="100%" alt="未找到资源...">
              </div>
              <!--上传头像-->
              <el-upload :action="uploadUrl(scope.row.id)"
                         :before-upload="beforeAvatorUpload"
                         :on-success="handleAvatorSuccess">
                <el-button type="primary" size="mini">更新图片</el-button>
              </el-upload>
            </template>
          </el-table-column>

//后台controller实现
    /**
     * 更新歌手头像
     */
    @PostMapping("/updateSingerPicture")
    @ApiOperation(value = "更新图片", notes = "更新歌手头像")
    public String updateSingerPicture(@RequestParam("file")MultipartFile file, @RequestParam("id") Integer id) {

        HashMap<String, Object> map = new HashMap<>();
        if (file.isEmpty()) {
            map.put(Constant.CODE, 0);
            map.put(Constant.MSG, "文件上传失败...");
            return JSON.toJSONString(map);
        }
        //文件名 = 当前时间到毫秒 + 原来的文件名
        String filename = System.currentTimeMillis() + file.getOriginalFilename();
        //文件路径
        String filepath = System.getProperty("user.dir")
                            + System.getProperty("file.separator")
                            + "music-server"
                            + System.getProperty("file.separator")
                            + "data"
                            + System.getProperty("file.separator")
                            + "img"
                            + System.getProperty("file.separator")
                            + "singerPic";
        //如果文件路径不存在,新建该路径
        File file1 = new File(filepath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filepath + System.getProperty("file.separator") + filename);
        //存储到数据库中的相对文件地址
        String storeAvatorPath = "data/img/singerPic/" + filename;
        try {
            file.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPicture(storeAvatorPath);
            int result = singerService.updateSinger(singer);
            if (result > 0) {
                map.put(Constant.CODE, 1);
                map.put(Constant.MSG, "上传成功...");
                map.put("picturePath", storeAvatorPath);
                return JSON.toJSONString(map);
            }else {
                map.put(Constant.CODE, 0);
                map.put(Constant.MSG, "上传失败...");
                return JSON.toJSONString(map);
            }
        } catch (IOException e) {
            map.put(Constant.CODE, 0);
            map.put(Constant.MSG, "上传失败..." + e.getMessage());
            return JSON.toJSONString(map);
        }
    }

携带表单的文件上传

    <el-form-item prop="url" label="歌曲上传" size="mini">
          <el-upload
            class="upload-demo"
            :on-success="handleMusicSuccess"
            accept="mp3"
            :action="materialMusicAndText()"
            :auto-upload="false"
            :before-upload="beforeUpload"
            ref="upload"
            :data="songAddForm"
            multiple>
            <el-button size="small">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item style="margin-left: 15%;">
          <el-button type="primary" size="mini" @click="addSong('songAddForm')">添加</el-button>
          <el-button size="mini" @click="resetAddForm">重置</el-button>
        </el-form-item>

 //后台controller实现
    @PostMapping("/insertSong")
    @ApiOperation(value = "添加歌曲", notes = "添加歌曲信息")
    public String insertSong(Song song, @RequestParam("file") MultipartFile file) {

        HashMap<String, Object> map = new HashMap<>();
        //上传歌曲文件
        if (file.isEmpty()) {
            map.put(Constant.CODE, 0);
            map.put(Constant.MSG, "歌曲上传失败...");
            return JSON.toJSONString(map);
        }
        //文件名 = 当前时间到毫秒 + 原来的文件名
        String filename = System.currentTimeMillis() + file.getOriginalFilename();
        //文件路径
        String filepath = System.getProperty("user.dir")
                + System.getProperty("file.separator")
                + "music-server"
                + System.getProperty("file.separator")
                + "data"
                + System.getProperty("file.separator")
                + "song";
        //如果文件路径不存在,新建该路径
        File file1 = new File(filepath);
        if (!file1.exists()) {
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filepath + System.getProperty("file.separator") + filename);
        //存储到数据库中的相对文件地址
        String storeSongPath = "data/song/" + filename;
        try {
            file.transferTo(dest);
            song.setUrl(storeSongPath);
            int result = songService.insertSong(song);
            if (result > 0) {
                map.put(Constant.CODE, 1);
                map.put(Constant.MSG, "添加成功...");
                map.put("storeSongPath", storeSongPath);
                return JSON.toJSONString(map);
            }else {
                map.put(Constant.CODE, 0);
                map.put(Constant.MSG, "添加失败...");
                return JSON.toJSONString(map);
            }
        } catch (IOException e) {
            map.put(Constant.CODE, 0);
            map.put(Constant.MSG, "添加失败..." + e.getMessage());
            return JSON.toJSONString(map);
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值