vue+axios+java实现文件上传功能

作者:铁打的阿秀 时间:2024-04-30 10:40:32 

本文实例为大家分享了vue+axios+java实现文件上传的具体代码,供大家参考,具体内容如下

背景

vue.js + axios + element前端,Java后台实现的文件上传,简单例子

前端代码

document.vue

<template>
? <div>
? ? <el-row class="action-row">
? ? ? <el-col :span="9">
? ? ? ? <el-button type="primary" icon="el-icon-plus" @click="add" size="medium">新增</el-button>
? ? ? </el-col>
? ? </el-row>
? ? <!-- 列表 -->
? ? <el-row>
? ? ? <el-table :data="docs" ref="docs" style="width: 100%" stripe @sort-change="sort" v-loading="loading">
? ? ? ? <el-table-column prop="docFileName" label="文件名称" sortable align="center" min-width="10%"></el-table-column>
? ? ? ? <el-table-column prop="docFileType" label="文件类型" sortable align="center" min-width="10%"></el-table-column>
? ? ? ? <el-table-column prop="createTime" label="上传时间" sortable align="center" min-width="10%"></el-table-column>
? ? ? </el-table>
? ? ? <div class="pagination">
? ? ? ? <el-pagination
? ? ? ? ? @size-change="handleSizeChange"
? ? ? ? ? @current-change="handlePageChange"
? ? ? ? ? :current-page="page"
? ? ? ? ? :page-size="limit"
? ? ? ? ? :total="total"
? ? ? ? ? :page-sizes="[10, 20, 50, 100]"
? ? ? ? ? layout="total, sizes, prev, pager, next, jumper"
? ? ? ? ? :background="true"
? ? ? ? ></el-pagination>
? ? ? </div>
? ? </el-row>
? ??
? ? <!-- 新建按钮弹出框 -->
? ? <el-dialog title="上传附件" :visible.sync="editvisible" :append-to-body="true" width="450px">
? ? ? <el-form :model="gtsDocument" :rules="rules" ref="gtsDocument" label-width="120px" label-position="left" size="small" class="edit-form">
? ? ? ? <el-form-item label="上传附件" prop="file">
? ? ? ? ? <el-upload ref="upload" action="doUpload" :limit="limitNum" :auto-upload="false" :on-change="fileChange" :on-exceed="exceedFile" :file-list="fileList">
? ? ? ? ? ? <el-button size="small" plain>选择文件</el-button>
? ? ? ? ? </el-upload>
? ? ? ? </el-form-item>
? ? ? </el-form>
? ? ? <div slot="footer" class="dialog-footer">
? ? ? ? <el-button @click="editvisible = false">取消</el-button>
? ? ? ? <el-button type="primary" @click="save">确定</el-button>
? ? ? </div>
? ? </el-dialog>
? </div>
</template>

<script>
import { list, del, create } from "@/api/gts/document";

export default {
? name: "GtsDocument",
? data() {
? ? return {
? ? ? editvisible: false, // 新增弹出框显示标识:默认不显示
? ? ? gtsDocument: {
? ? ? ? // 随附单据表单
? ? ? ? docType: "",
? ? ? ? docNo: "",
? ? ? ? gtsId: "",
? ? ? ? taskId: "",
? ? ? ? file: ""
? ? ? },
? ? ? isupdate: false,
? ? ? limitNum: 1,
? ? ? fileList: [],
? ? ? docs: [],
? ? ? loading: false,
? ? ? page: 1,
? ? ? limit: 10,
? ? ? total: 0,
? ? ? rules: {},
? ? };
? },
? created: function() {
? ? this.search();
? },
? methods: {
? ? search() {
? ? ? // 初始化列表?
? ? ? list(this.page, this.limit, this.$route.query.gtsId).then(v => {
? ? ? ? if ("ok" == v.data.msg) {
? ? ? ? ? this.docs = v.data.rows;
? ? ? ? ? this.total = v.data.total;
? ? ? ? }
? ? ? });
? ? },
? ? // 新增按钮点击事件
? ? add() {
? ? ? this.editvisible = true;
? ? ? this.$nextTick(() => {
? ? ? ? this.isupdate = false;
? ? ? ? this.$refs.gtsDocument.resetFields();
? ? ? });
? ? },
? ? // 文件超出个数限制时的校验
? ? exceedFile(files, fileList) {
? ? ? this.$notify.warning({
? ? ? ? title: this.$t("com.warning"),
? ? ? ? message: this.$t("com.onlySelect") + `${this.limitNum} ` + this.$t("com.someFile")
? ? ? });
? ? },
? ? // 文件状态改变时的事件
? ? fileChange(file, fileList) {
? ? ? this.gtsDocument.file = file.raw;
? ? },
? ? // 保存
? ? save() {
? ? ? this.$refs["gtsDocument"].validate(valid => {
? ? ? ? if (valid) {
? ? ? ? ? let formData = new FormData();
? ? ? ? ? let file = this.gtsDocument.file;
? ? ? ? ? formData.append("file", file);
? ? ? ? ? if (!file) {
? ? ? ? ? ? return this.$message.warning('请选择上传附件');
? ? ? ? ? }
? ? ? ? ? create(formData).then(resp => {
? ? ? ? ? ? if ("ok" == resp.data.msg) {
? ? ? ? ? ? ? this.editvisible = false;
? ? ? ? ? ? ? this.$message.success('数据保存成功');
? ? ? ? ? ? ? this.search();
? ? ? ? ? ? ? this.$refs.upload.clearFiles();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? this.$message.error('保存失败');
? ? ? ? ? ? }
? ? ? ? ? });
? ? ? ? }
? ? ? });
? ? },
? ? handlePageChange(i) {
? ? ? this.page = i;
? ? ? this.search();
? ? },
? ? handleSizeChange(i) {
? ? ? this.limit = i;
? ? ? this.search();
? ? },
? }
};
</script>

<style rel="stylesheet/css">
.setting-form .el-form-item__label {
? padding-right: 30px;
}
</style>

document.js

import http from '@/utils/request'
import axios from 'axios'

export function create(formData) {
? return axios({
? ? url: 'http://localhost:8080/solvay-ecustoms//gts/documents/add',
? ? method: 'post',
? ? data: formData,
? ? withCredentials: true // cros with cookie
? })
}
export function list(page, limit, id) {
? return http.post('gts/documents', { page, limit, id })
}

后台代码

controller层

?/**
? ? ?* <p>Description: 附件上传</p>
? ? ?* @param file ? ?上传附件
? ? ?* @return
? ? ?*/
? ? @ResponseBody
? ? @PostMapping("/documents/add")
? ? public String addAttach(@RequestParam("file") MultipartFile file) throws IOException {
? ? ? ? // 获取文件名称
? ? ? ? String fileName = file.getOriginalFilename();
? ? ? ? if (StringUtils.isBlank(fileName)) {
? ? ? ? ? ? return jsonfailed("文件不能为空");
? ? ? ? }
? ? ? ? // 获取文件的大小
? ? ? ? long fileSize = file.getSize();
? ? ? ? if (fileSize > 10 * 1024 * 1024) {
? ? ? ? ? ? return jsonfailed("上传文件大小超出限定大小10M");
? ? ? ? }
? ? ? ? // 获取文件的扩展名
? ? ? ? // String extension = FilenameUtils.getExtension(fileName);
? ? ? ? // 获取配置路径
? ? ? ? String path = "D:/home/ecustoms/upload/";
? ? ? ? String newPath = path + UUID.randomUUID().toString().replaceAll("-", "") + "\\";
? ? ? ? File newDir = new File(newPath);
? ? ? ? if (!newDir.exists()) {
? ? ? ? ? ? newDir.mkdirs(); // 目录不存在的情况下,创建目录
? ? ? ? }
? ? ? ? File newFile = new File(newDir, fileName);
? ? ? ? //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
? ? ? ? file.transferTo(newFile);
? ? ? ? return "ok";
?}

实现截图如下

vue+axios+java实现文件上传功能

vue+axios+java实现文件上传功能

vue+axios+java实现文件上传功能

来源:https://blog.csdn.net/zcxbd/article/details/89404600

标签:vue,axios,java,文件上传
0
投稿

猜你喜欢

  • Python内存管理方式和垃圾回收算法解析

    2022-09-10 17:49:11
  • oracle 存储过程加密的方法

    2009-03-06 10:58:00
  • 利用Python编写一个闹钟,治好你的拖延症

    2021-11-15 12:06:48
  • Python获取时间范围内日期列表和周列表的函数

    2023-03-25 15:16:39
  • python连接clickhouse数据库的两种方式小结

    2024-01-24 19:25:02
  • SQL Server中应当怎样得到自动编号字段

    2009-01-15 12:48:00
  • golang如何修改json文件内容的方法示例

    2024-04-26 17:32:44
  • Python 一行代码能实现丧心病狂的功能

    2023-07-25 08:32:17
  • Python IO文件管理的具体使用

    2023-04-06 01:54:47
  • 在VSCode中如何配置Python开发环境

    2023-05-13 22:02:59
  • 通过自学python能找到工作吗

    2021-07-24 04:26:33
  • 简单的Python调度器Schedule详解

    2021-09-15 09:49:19
  • PHP hebrev()函数用法讲解

    2023-06-03 12:49:32
  • Django基础三之视图函数的使用方法

    2022-12-14 07:23:41
  • python实现k均值算法示例(k均值聚类算法)

    2021-04-24 20:27:39
  • python基本数据类型练习题

    2022-07-21 14:26:26
  • Tensorflow tf.nn.atrous_conv2d如何实现空洞卷积的

    2023-03-29 11:05:43
  • php获取给定日期相差天数的方法分析

    2024-05-02 17:34:20
  • Symfony2框架创建项目与模板设置实例详解

    2023-11-20 23:36:39
  • python matplotlib实现条形图的填充效果

    2022-03-12 07:55:20
  • asp之家 网络编程 m.aspxhome.com