饿了么Element UI之Upload组件图片上传【原创】
图片文件换汤不换药,只要注意前端的写法即可
1.饿了么组件可以利用 http-request 的属性对上传进行自定义 :http-request="uploadFile"
2.设置文件FormData对象传入请求
const formdata = new FormData();
const file = params.file;
formdata.append("file", file);
3.全部代码
<template>
<div style="margin-top:5%">
<el-upload
:onError="uploadError"
:onSuccess="uploadSuccess"
class="upload-demo"
ref="upload"
:auto-upload="false"
accept=".eml"
multiple
:before-upload="beforeUpload"
action=" "
:http-request="uploadFile"
>
<el-button slot="trigger" size="small" type="primary">----</el-button>
<el-button slot="trigger" size="small" type="primary">----</el-button>
<el-button slot="trigger" size="small" type="primary">----</el-button>
<el-button slot="trigger" size="small" type="primary">----</el-button>
<el-button slot="trigger" size="small" type="primary">选取邮件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传邮件</el-button>
<div slot="tip" class="el-upload__tip">可以同时上传多个eml文件至服务器</div>
</el-upload>
</div>
</template>
<script>
import axios from "axios";
export default {
data() {
return {};
},
computed: {
//进入的用户
userSO_json() {
let userSO_json = JSON.parse(sessionStorage.getItem("userSO_json"));
return userSO_json;
}
},
methods: {
/**
* 自定义:http-request="uploadFile"之后,回调方法暂且失效,所以提示信息应该写在axios里面
*/
// 上传成功后的回调
uploadSuccess(response, file, fileList) {
console.log("上传文件", response);
this.$message({
showClose: true,
message: "恭喜你,邮件上传成功",
type: "success"
});
},
// 上传错误后的回调
uploadError(err, file, fileList) {
if (err.message != "") {
this.$message.warning(err.message);
} else {
this.$message.warning("网络错误,不能访问");
}
},
uploadFile(params) {
const formdata = new FormData();
const file = params.file;
formdata.append("file", file);
axios
.post("/api/EmlUploadController/uploadFile?userName="+this.userSO_json.userName, formdata, {
headers: {
//头部信息
"Content-Type": "multipart/form-data",
token: this.userSO_json.token
}
})
.then(response => {
this.$message({
showClose: true,
message: "恭喜你,邮件上传成功",
type: "success"
});
})
.catch(error => {
//前端的token留在点击退出按钮那里删除,这里就只是提示过期
if (error.message != "") {
this.$message.warning("此封一模一样邮件你已经上传过了");
} else {
this.$message.warning("后端token过期,请重新登录");
}
});
},
//添加任务
async beforeUpload(file) {
console.log("beforeUpload");
// const extension = file.name.split(".")[1] === "eml";
// const isLt2M = file.size / 1024 / 1024 < 10;
// if (!extension) {
// console.log("上传邮件只能是 eml格式!");
// }
// if (!isLt2M) {
// console.log("上传邮件大小不能超过 10MB!");
// }
// return extension && isLt2M;
}, //提交
submitUpload() {
this.$refs.upload.submit();
}
}
};
</script>
<style scoped>
.el-table--border,
.el-table--group {
border: none;
}
.el-table__header-wrapper th:nth-last-of-type(2) {
border-right: none;
}
.el-table--border td:nth-last-of-type(1) {
border-right: none;
}
.el-table--border::after,
.el-table--group::after {
width: 0;
}
.el-select .el-input {
width: 180px;
}
.input-with-select .el-input-group__prepend {
background-color: #fff;
}
.input-with-select {
margin-left: -5px;
}
.select {
margin-left: 0px;
}
.input-with-select {
background-color: #fff;
width: 390px;
}
.pagination {
height: 80px;
text-align: center;
}
.choose {
float: left;
}
.upload-demo {
float: left;
}
.button1 {
left: 40%;
}
.button2 {
text-align: center;
}
.divider {
margin: 0;
}
.conditionalQuery {
float: right;
height: 60px;
margin-right: 80px;
}
</style>
饿了么Element UI之Upload组件图片上传【原创】的更多相关文章
- vue问题三:element ui的upload组件上传图片成功和移除事件
element ui的upload组件上传图片成功和移除事件: 登录后获取到后台传的token存到中: sessionStorage.setItem("token",data.ob ...
- 前端element ui 文件base64加密字符串 上传
<el-form-item label="附件" prop="attachment"> <el-upload :multiple=" ...
- UI组件--element-ui--Upload多组件自定义上传
需求: 提交详细信息的表单, 并上传对应图片(如下图), 后台接口要求表单数据和图片需要一次上传完成.. 分析: 实际上, 每个element-ui Upload组件都应发送一次请求, 很明显不符合我 ...
- antd组件Upload实现自己上传
前言 在实现图片上传时,可能需要用到Upload,但是它默认的上传方式是加入图片后直接上传,如果要实现最后再一次性上传,需要自定义内容. //添加按钮的样式 const uploadButton = ...
- 06.LoT.UI 前后台通用框架分解系列之——浮夸的图片上传
LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...
- Jquery图片上传组件,支持多文件上传
Jquery图片上传组件,支持多文件上传http://www.jq22.com/jquery-info230jQuery File Upload 是一个Jquery图片上传组件,支持多文件上传.取消. ...
- vue图片上传组件
前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...
- 图片上传组件webuploader
前端组件webuploader 当时也是搞了很久参考这种demo,但是没记.现在事后大致总结下.直接上大概代码(我使用asp.net MVC来做的): 执行顺序:(get)Record/Add——A ...
- 图片上传--Upload
图片上传--Upload 图片上传基于spring框架写的代码: 1.首先:我们要再springmvc中添加试图解析器: <!-- 图片解析器 --> <bean id=" ...
- MVC文件上传05-使用客户端jQuery-File-Upload插件和服务端Backload组件自定义上传文件夹
在零配置情况下,文件的上传文件夹是根目录下的Files文件夹,如何自定义文件的上传文件夹呢? MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 MVC文 ...
随机推荐
- vscode 设置窗口菜单栏显示字体大小
最近换了一块大些的显示屏,发现vscode的窗口字体有些小了,不是很方便,于是研究了一下如何设置vscode的窗口字体大小. 需要注意的是这里的设置是对窗口字体的而不是编辑器的字体. 1 . 通过主 ...
- 代码随想录Day6
454.四数相加Ⅱ 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, j, k ...
- vue-表单与v-model
使用 v-model 后,表羊控件显示的值只依赖所绑定的数据,不再关心初始化时的 value 属性,对于 textarea></textarea> 之间插入的值,也不会生效. 使用 ...
- 13. Advanced-control timers (TIM1 and TIM8)
1. 基本介绍 有三个基础的寄存器: 计数寄存器(TIMx_CNT,Counter register) 预分频寄存器(TIMx_PSC,Prescaler register) 自动重载寄存器(TIMx ...
- 瑞芯微 | 如何固定以太口地址为指定ip?ifplugd妙用
rxw的RK3568的evb1公板,有2个以太口, 默认UI界面只能配置eth0,无法配置eth1, 实际应用中,有时需要一旦有网线插入,就需要该地址设置为指定IP地址. 本文介绍2个最简单的方法实现 ...
- Atcoder ABC364 D-F
Atcoder ABC364 D-F D - K-th Nearest 链接: D - K-th Nearest (atcoder.jp) 简要题意: 问题陈述 在一条数线上有 \(N+Q\) 个点 ...
- 异常处理,内置方法(__new__,__init__,__del__析构方法,单例模式,item系列)
__new__ 创建一个对象 class A: def __init__(self): print('in init') def __new__(cls): print('in new') self= ...
- zabbix基本概念
Zabbix是一个企业级的.开源的.分布式监控解决方案. Zabbix可以监控网络和服务的监控状况. Zabbix利用灵活的告警机制,允许用户对事件发送基于Email的告警. 这样可以保证快速的对问题 ...
- 关于封装axios报错Cyclic dependency的问题
在npm start的时候直接报错Cyclic dependency循环依赖 JS 循环依赖 (require cycle)_腾飞日记-CSDN博客_js循环依赖 可以看上面的博文了解一下这个错大概在 ...
- React的prop-types下载安装教程
最近刚入门react,所有react的资源都是从本地导入的,这就难免要去网上找要用的包,react包和reactdom,还有babel的包都挺好找的,官网就有现成的可以用,但是prop-types包貌 ...