ui中uploadui组件显示病毒如何传递文件及其他参数

在vue项目中使用element-ui的Upload上传组件的示例
转载 &更新时间:日 10:30:31 & 作者:三问静先生
本篇文章主要介绍了在vue项目中使用element-ui的Upload上传组件的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文介绍了vue项目中使用element-ui的Upload上传组件的示例,分享给大家,具体如下:
&el-upload
class='ensure ensureButt'
:action="importFileUrl"
:data="upLoadData"
name="importfile"
:onError="uploadError"
:onSuccess="uploadSuccess"
:beforeUpload="beforeAvatarUpload"
&el-button size="small" type="primary"&确定&/el-button&
其中importFileUrl是后台接口,upLoadData是上传文件时要上传的额外参数,uploadError是上传文件失败时的回掉函数,uploadSuccess是文件上传成功时的回掉函数,beforeAvatarUpload是在上传文件之前调用的函数,我们可以在这里进行文件类型的判断。
importFileUrl: 'http:dtc.com/cpy/add',
upLoadData: {
cpyId: '123456',
occurTime: '2017-08'
methods: {
// 上传成功后的回调
uploadSuccess (response, file, fileList) {
console.log('上传文件', response)
// 上传错误
uploadError (response, file, fileList) {
console.log('上传失败,请重试!')
// 上传前对文件的大小的判断
beforeAvatarUpload (file) {
const extension = file.name.split('.')[1] === 'xls'
const extension2 = file.name.split('.')[1] === 'xlsx'
const extension3 = file.name.split('.')[1] === 'doc'
const extension4 = file.name.split('.')[1] === 'docx'
const isLt2M = file.size / 1024 / 1024 & 10
if (!extension && !extension2 && !extension3 && !extension4) {
console.log('上传模板只能是 xls、xlsx、doc、docx 格式!')
if (!isLt2M) {
console.log('上传模板大小不能超过 10MB!')
return extension || extension2 || extension3 || extension4 && isLt2M
最近在适用VUE作为前端框架做自己的项目,在做到需要上传文件到服务器时,同事告诉我upload之中的action也就是上传地址不能动态的去更改,然后去看了一下,需要做以下处理才能动态的使用:
action是一个必填参数,且其类型为string,我们把action写成:action,然后后面跟着一个方法名,调用方法,返回你想要的地址,代码示例:
//html 代码
&el-upload :action="UploadUrl()" :on-success="UploadSuccess" :file-list="fileList"&
&el-button size="small" type="primary" &点击上传&/el-button&
&div slot="tip" class="el-upload__tip"&&/div&
&/el-upload&
// js 代码在 methods中写入需要调用的方法
UploadUrl:function(){
return "返回需要上传的地址";
这是我解决的方法,希望能帮到需要的人
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具404 Not Found
The requested URL /q/2049/a-4149 was not found on this server.我们在一个列表中分别都需要有upload组件的时候也就涉及到了多个upload同时存在;
因为一般可以在success回调中拿到上传成功的图片已经成功的response,多个也可以,这个没毛病;
文档如下:
!!!但是,当多个同类型的upload同时存在的时候,我在怎么知道回调里面的fileList该与谁关联呢?
例如这种情况
这两个甚至无数个“步骤”,对应的是vue data 里面的某个数组,如:
    {      图图图
    },    {      图    }
假设这个时候我们点击第一个BOX里面的图片上传,然后拿到对应的对调参数,(response,file,fileList)
说白了,我不知道该把这个fileList给谁处理,是this.arr[0] , 还是 this.arr[1]。
一般项目中处理这种类似问题的时候是把对应的item通过这个函数传过来
:on-success="handleProcedureSuccess(item)"
不过显然不行,如果我传实参了,那么组件内定义的几个参数就过不来了,覆盖了;
然后我就想这,我非得把当前item给传到这个方法里去,不然没法操作。
无奈····,改改改,经过我努力搜寻发现要修改这个文件
在项目里面找到node_modules/element-ui/lib/element-ui.common.js
我们在props里面加一个要父组件传过来的参数,随便叫啥啦
然后继续往下找到,this.onSuccess的执行地点,加上这个参数
好了这里改好了,再到我们的页面里,写组件的时候多传个参数,我这里传的是数组的索引
&OK啦,再然后在success回调的时候就会多出一个参数,就是我们传进去这个自定义的值啦
由此,用第四个参数去决定你处理的是第几个数组元素。
=========================华丽的分割线==============================
后面想了想还是不应该通过这种方式去解决问题,为什么呢,因为你要去修改包文件才能够达到
想要的效果,那么就会导致下次部署项目环境的时候也需要改,或者其他同事协同工作的时候还
需要他也去改一下才能正常运行····so,不合理。
后面又想到一个解决方案就是自己对它的upload组件进行二次封装,我们不动他自己的代码,只
是在调用自己upload组件的时候多传一个参数,然后在执行回调的时候返回回来即可,好了直接
上图:最它做了一个简单的封装
好啦,正确的解决思路引导
阅读(...) 评论()layui.upload上传文件的同时怎么传递一个参数_百度知道
layui.upload上传文件的同时怎么传递一个参数
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
在 before 回调里面 执行追加 隐藏input的操作,里面放你的参数 $('.layui-upload-file').after( '' ); 这种方式只能传递静态参数,对于select的值好像获取不到
采纳率:89%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。&el-table-column label="操作"&
&template slot-scope="scope"&
&el-button icon="el-icon-circle-plus-outline" type="primary" v-on:click="addOp(scope.row)"&&/el-button&
&el-button type="primary" v-on:click="importQuato(scope.row)"&导入额度批次表&/el-button&
&/template&
&/el-table-column&
&el-button type="primary" v-on:click="importQuato(scope.row)"&导入额度批次表&/el-button&//导入按钮
scope.row可以获取每一列的id  
&el-dialog :title="title" :visible.sync="dialogVisible"&
&el-upload
class="upload-demo"
class='ensure ensureButt'
action="123" //这里可以随意不影响
:before-upload="beforeAvatarUpload" //上传前文件校验
&i class="el-icon-upload"&&/i&
&div class="el-upload__text"&将文件拖到此处,或&em&点击上传&/em&&/div&
&div class="el-upload__tip" slot="tip"&只能上传xls、xlsx文件,且不超过10MB&/div&
&/el-upload&
&div slot="footer" class="dialog-footer"&
&!--&el-button v-on:click="dialogVisible = false"&取 消&/el-button&--&
&el-button type="primary" v-on:click="dialogVisible = false"&确 定&/el-button&
&/el-dialog&  
// 上传前对文件的大小的判断
beforeAvatarUpload (file) {
var fileName=new Array()
fileName =file.name.split('.');
const extension = fileName[fileName.length-1] === 'xls'
const extension2 =
fileName[fileName.length-1]=== 'xlsx'
const isLt2M = file.size / 1024 / 1024 & 10
if (!extension && !extension2) {
this.$message({
message: '上传模板只能是xls、xlsx格式!',
type: 'warning'
console.log('上传模板只能是xls、xlsx格式!')
if (!isLt2M) {
this.$message({
message: '上传模板大小不能超过 10MB!',
type: 'warning'
console.log('上传模板大小不能超过 10MB!')
if (extension || extension2 && isLt2M == true) {
console.log(file)
let fd = new FormData()
fd.append('invoiceTypeId', this.invoice_type_id)//随文件上传的其他参数
fd.append('epid', this.epid)
fd.append('file', file)
// console.log(fd)
this.newImport(fd).then(function (res) {//校验完成后提交
console.log(res)
}, function () {
console.log('failed');
return true
return extension || extension2 && isLt2M
//提示信息
open: function (msg, code) {
if (code == '000') {
this.$alert(msg, '提示', {
confirmButtonText: '确定',
type: 'success',
callback: action =& {
this.dialogFormVisible = false;
location.reload();
this.$alert(msg, '提示', {
confirmButtonText: '确定',
type: 'error',
callback: action =& {
this.dialogFormVisible = false;
location.reload();
newImport (data) {
this.$http.post('../enterPriseQuota/importEnterPriseQuota', data).then(function (res) {//成功后回调
let code = res.data.//返回json结果
let msg = res.data.
this.open(msg, code);
console.log('success');
}, function () {
console.log('failed');
@RequestMapping("/importEnterPriseQuota")
@ResponseBody
public Map importEnterPriseQuota(@RequestParam(value = "invoiceTypeId") String invoiceTypeId,
@RequestParam(value = "epid") String epid,
@RequestParam("file") MultipartFile proFile, HttpServletRequest request) {
String fileDir = request.getSession().getServletContext().getRealPath("/tmp");
File dir = new File(fileDir);
Map resMap = null;
File file = null;
file = new File(fileDir, proFile.getOriginalFilename());
if (!dir.exists()) {
dir.mkdir();
if (!file.exists()) {
file.createNewFile();
proFile.transferTo(file);
Date a = new Date();
resMap = enterPriseQuotaService.importEnterPriseQuato(invoiceTypeId,file,epid);
Date b = new Date();
log.info("************all_time*************************" + (b.getTime() - a.getTime()));
return resM
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null && file.exists()) {
file.delete();
resMap.put("returncode", "999");
resMap.put("msg", "程序异常,请联系管理员");
return resM
&el-dialog :title="tagName" :visible.sync="dialogVisible"&
&el-upload
class="upload-demo"
class='ensure ensureButt'
:action="importFileUrl"//在初始时指定url地址即可
:on-error="uploadError"
:on-success="uploadSuccess"
:before-upload="beforeAvatarUpload"
&i class="el-icon-upload"&&/i&
&div class="el-upload__text"&将文件拖到此处,或&em&点击上传&/em&&/div&
&div class="el-upload__tip" slot="tip"&只能上传xls、xlsx文件,且不超过10MB&/div&
&/el-upload&
&div slot="footer" class="dialog-footer"&
&!--&el-button v-on:click="dialogVisible = false"&取 消&/el-button&--&
&el-button type="primary" v-on:click="dialogVisible = false"&确 定&/el-button&
&/el-dialog&
//有时候 :on-success,:on-error 这个函数会无法调用,之前看另一个帖子是用的:onError="uploadError" :onSuccess="uploadSuccess"
:&昨天又遇到上传成功但是无法调用成功回调函数的问题,这里涉及到vue的生命周期,导致无法调用,js也不会报错,把对应函数放到methods顶部可解决。
// 上传成功后的回调
uploadSuccess (response) {
let code = response.
let msg = response.
this.open(msg, code);
// 上传错误
uploadError (response) {
this.open("500", "文件导入异常!");
@RequestMapping("inEmployee")
@ResponseBody
public Map inEmployee(HttpServletRequest servletRequest) {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletR
Iterator&String& itr = request.getFileNames();
MultipartFile proFile = null;
while (itr.hasNext()) {
String str = itr.next();
proFile = request.getFile(str);
String fileDir = request.getSession().getServletContext().getRealPath("/tmp");
File dir = new File(fileDir);
Map resMap = null;
File file = null;
file = new File(fileDir, proFile.getOriginalFilename());
if (!dir.exists()) {
dir.mkdir();
if (!file.exists()) {
file.createNewFile();
proFile.transferTo(file);
Date a = new Date();
resMap = employeeService.insEm(file,fileDir);
Date b = new Date();
log.info("************all_time*************************" + (b.getTime() - a.getTime()));
return resM
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null && file.exists()) {
file.delete();
resMap.put("returncode", "999");
resMap.put("msg", "程序异常,请联系管理员");
return resM
阅读(...) 评论()
来访总数:&&人}

我要回帖

更多关于 jquery ui组件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信