vue+axios+java实现文件上传功能
作者:铁打的阿秀 发布时间:2024-04-30 10:40:32
标签:vue,axios,java,文件上传
本文实例为大家分享了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";
?}
实现截图如下
来源:https://blog.csdn.net/zcxbd/article/details/89404600


猜你喜欢
- 链接的 target 属性怎么用 JS 来控制? 在HTML 4.0 Strict和XHTML 1.0 STRICT里不允许在<a&g
- 本文实例讲述了Python对列表排序的方法。分享给大家供大家参考。具体分析如下:1、sort()函数sort()函数使用固定的排序算法对列表
- 本文实例讲述了Python接收Gmail新邮件并发送到gtalk的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env
- PHP str_split() 函数实例把字符串 "Hello" 分割到数组中:<?php print_r(str
- Protocol 和服务器一样,也是通过该类来实现。先看一个简短的例程:from twisted.internet.protoc
- 本文实例为大家分享了python读取mysql数据绘制条形图的具体代码,供大家参考,具体内容如下Mysql 脚本示例:create tabl
- 本文实例讲述了Python基础之循环语句用法。分享给大家供大家参考,具体如下:while 循环Python中while语句的一般形式:whi
- 前言为了让大家更好的理解本期知识点,先介绍以下几个知识点:线性结构、非线性结构、循环、迭代、遍历、递归。线性结构:数组、队列非线性结构:树、
- Python练习内容:SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Pyth
- <?php//所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素。由于采用了Bit为单位来存储
- 今天主要向大家讲述的是优化SQL Server数据库的实际操作经验的总结,同时也有对其优化的实际操作中出现的一些问题的描述,以及对SQL S
- 1. 简述我们在用scrapy爬取数据时,首先就要明确我们要爬取什么数据。scrapy提供了Item对象这种简单的容器,我们可以通过Item
- Python程序中,在进程和进程之间是不共享全局变量的数据的。我们来看一个例子:from multiprocessing import Pr
- 上一篇博文,我们已经顺利的从cnodejs.org请求到了数据,但是大家可以注意到我们的/src/api/index.js的第一句就是://
- 1. 图像轮廓1.1 findContours介绍cv2.findContours(img, mode, method)mode:轮廓检索模
- 字典的常用方法方便举例,先创建2个字典list_test={"bob":19,"aoa":18,&q
- 在用Linux(OS:Centos 7.2)时看到有一行代码是:export PYTHONPATH=$PYTHONPATH:/home/us
- python实现的五子棋,能够自动判断输赢,没有是实现电脑对战功能源码下载:pygame五子棋# 1、引入pygame 和 pygame.l
- 中间件Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出。中间件的设
- 一、安 * rew终端上运行 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubus