前端上传文件+参数发送请求的正确方式(springboot+vue2)

因为表格中有文件类型的上传与下载,在上传请求中需要给到记录Id,在此记录一下正确方式。

在这里插入图片描述
效果图:

在这里插入图片描述

一、前端发送请求

      <!--文件操作-->
      <el-table-column align="center" v-if="fileActionShow" :fixed="addBtnList.fixed" :label="fileAction.name" :min-width="fileAction.width">
        <template slot-scope="scope">

          <el-button @click="clickCall('handleImport', scope)">
            <el-upload class="upload-demo" :action="fileAction.fileUploadUrl" :show-file-list="false" :limit="1" :multiple="false" :auto-upload="true" accept=".xml" :before-upload="beforeUpload"
              :headers="fileAction.uploadHeaders" :data="fileAction.uploadData">上传
            </el-upload></el-button>
          <el-button @click="clickCall('handleExport', scope)"> <a :href="fileAction.fileUploadUrl">导出</a></el-button>
        </template>
      </el-table-column>
import Cookie from "js-cookie";


data:
      fileAction: {
        width: 60,
        name: "XML文件",
        fileUploadUrl: "/*****/importXml",
        fileExportUrl: "/*****/exportXml",
        uploadData: {},
        uploadHeaders: {},
      },
method:
    // 文件上传操作
    handleImport(row) {
      this.fileAction.uploadData = {
        id: row.id,
      };
      // Token一定要加,否则报401,因为这是自己写的请求,不是封装的,不会自带token
      this.fileAction.uploadHeaders = {
        Authorization: Cookie.get("droneToken"),
      };
    },
    //上传之前
        beforeUpload(file, fileList) {
      let promise = new Promise((resolve) => {
        this.$nextTick(function () {
          resolve(true);
        });
      });
      return promise;
    },

二、后端代码实现

Controller层

    @ApiOperation("导入数据")
    @PostMapping("importXml")
    public Result importData(@RequestParam(value="file") MultipartFile file, @RequestParam(value="id") Long id) throws Exception {
        return routeService.importData(file, id);
    }

当然,如果存储文件到数据库Blog类型,笔者采用PreparedStatement存储

    @Override
    public Result importData(MultipartFile file, Long id) throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
             connection = new JDBCConnection().getConnection();
            String sql = "update xxx set file_txt=? where id=?";
             ps = connection.prepareStatement(sql);
            InputStream inputStream = file.getInputStream();
            ps.setBlob(1,inputStream);
            ps.setLong(2,id);
            ps.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            connection.close();
            ps.close();
        }
        return Result.success("上传成功");
    }

JDBCConnection:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCConnection {
    private static final String DBDRIVER="com.mysql.cj.jdbc.Driver";
    private static final String DBURL = "填自己的地址";
    public static final String USERNAME="填自己的账号";
    public static final String PASSWORD = "填自己的密码";
    private Connection conn;

    public JDBCConnection() {
        //加载驱动
        try {
            Class.forName(DBDRIVER);
            //获取连接
            conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public Connection getConnection() throws SQLException {
        if(conn==null||conn.isClosed()){
            conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
        }
        return  this.conn;
    }

    public void close(){
        if(this.conn!=null){
            try {
                this.conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

效果图:
在这里插入图片描述
在这里插入图片描述
存储成功,大兴西北。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值