文章目录
使用上传组件需要引入并注册Upload组件而这个组件中又用到了Button,所以也需要注册一下Button,我们只需要在main.js(new Vue实例的文件里边),加上下边的代码
//引入
import import { Upload , Button } from 'element-ui'
//注册
Vue.component(Upload.name, Upload)
Vue.component(Button.name, Button)
官网中有好几种上传的代码,但是总的来说就两种,一种是直接上传的,一种是手动上传的,其他的就是样式稍微变了一下,用法什么的都差不多,所以这里只复制了一下自动上传和手动上传的两个例子的代码来说明一下element-ui提供给我们的一些方法的使用
| 属性名 | 类型 | 作用 |
|---|---|---|
| action(必选) | 字符串 | 指定提交的接口 |
| multiple | 布尔值 | 是否支持多文件上传 |
| drag | 布尔值 | 是否启用拖拽 |
| on-preview | 函数 | 点击文件列表中已上传的文件时执行的函数 |
| on-remove | 函数 | 文件列表移除文件时执行的函数 |
| on-success | 函数 | 文件上传成功时执行的函数 |
| on-error | 函数 | 文件上传失败时执行的函数 |
| on-change | 函数 | 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 |
| before-upload | 函数 | 上传文件之前执行的函数 |
| before-remove | 函数 | 删除文件之前执行的函数 |
| auto-upload | 布尔值 | 是否选取文件后立即进行上传 |
| file-list | 数组 | 上传的文件列表 |
| http-request | 函数 | 覆盖掉默认上传的行为 |
| disabled | 布尔值 | 是否禁用 |
| limit | 数字 | 最大允许上传个数 |
| on-exceed | 函数 | 超出个数限制时执行的函数 |
其中的一些函数我们在自动上传中不好说明,所以上边的一些属性和方法的用法演示主要在自定义上传中演示
在上传的文件中的官方代码中我们不仅用到Upload组件和Button组件,同时还需要引入提示信息的一个的一个模块和提示信息的一个模块
只需要在上边组件使用的代码修改为
//引入
import import { Upload , Button , MessageBox ,Message } from 'element-ui'
//注册
Vue.component(Upload.name, Upload)
Vue.component(Button.name, Button)
//往Vue原型中添加方法,这样的话我们才能在全局里边通过this来直接访问到
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message=Message
上边往Vue原型中添加方法我们用到了Vue原型中的一个关系,即VueComponent.prototype.proto===Vue.prototype
同时MessageBox中不仅包含我们用到的confim也包含其他的一些提示类的方法
this.$confim()会出现一个带确定和取消按钮的弹窗,同时函数执行会返回一个promise对象,我们可以在其.then方法中执行点击确定的回调,在.catch中执行取消的回调
this. m e s s a g e . x x x ( ) 会 出 现 一 个 提 示 框 , 提 示 框 的 类 型 分 为 四 种 , 分 别 是 成 功 ( x x x 的 值 是 s u c c e s s ) , 错 误 ( x x x 的 值 是 e r r o r ) 、 警 告 ( x x x 的 值 是 w a r n i n g ) 以 及 消 息 ( 不 写 m e s s a g e 后 边 的 东 西 ) , 我 们 通 过 修 改 message.xxx()会出现一个提示框,提示框的类型分为四种,分别是成功(xxx的值是success),错误(xxx的值是error)、警告(xxx的值是warning)以及消息(不写message后边的东西),我们通过修改 message.xxx()会出现一个提示框,提示框的类型分为四种,分别是成功(xxx的值是success),错误(xxx的值是error)、警告(xxx的值是warning)以及消息(不写message后边的东西),我们通过修改message点后边的的值,当然我们这里只是说简单的说明一下我们这里用到的,想要了解具体的可以看官方文档去细致的了解一下
直接上传顾名思义就是我们上传图片的时候,上传完它就会"立即"上传给action所指定的url连接
<template>
<el-upload
class="upload-demo"
action=""
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
multiple
:on-success="handleSuccess"
:on-error="handleError"
:on-change="handelChange"
:limit="3"
:on-exceed="handleExceed"
:file-list="fileList"
>
<el-button size="small" type="primary">点击上传</el-button>
<!-- 用于指定上传文件下的文字说明 -->
<div slot="tip" class="el-upload__tip">
只能上传jpg/png文件,且不超过500kb
</div>
</el-upload>
</template>
<script>
export default {
data() {
return {
//初始化上传列表
fileList: [],
};
},
methods: {
handelChange(file,fileList){
console.log("文件修改执行的函数", file, fileList);
this.fileList=fileList;
},
handleRemove(file, fileList) {
console.log("移除文件执行的函数", file, fileList);
this.fileList=fileList;
},
handlePreview(file) {
console.log("点击已经上传的文件", file);
},
handleSuccess() {
this.$message.success("上传成功");
},
handleError() {
this.$message.error("上传失败");
},
handleExceed(files, fileList) {
this.$message.warning(
`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
files.length + fileList.length
} 个文件`
);
},
beforeRemove(file, fileList) {
console.log('移除之前执行的函数',fileList);
return this.$confirm(`确定移除 ${file.name}?`);
},
},
};
</script>
<style>
</style>
<template>
<div>
<el-upload
class="upload-demo"
ref="upload"
action=""
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:on-change="handelChange"
name="cover"
:multiple="true"
:auto-upload="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<div slot="tip" class="el-upload__tip">
只能上传jpg/png文件,且不超过500kb
</div>
</el-upload>
<el-button @click="handelSend">上传</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fileList: [],
};
},
methods: {
handelChange(file, fileList) {
this.fileList = fileList;
console.log("文件修改执行的函数", file, fileList);
},
handleRemove(file, fileList) {
console.log("移除文件执行的函数", file, fileList);
this.filesList = fileList;
},
handlePreview(file) {
console.log("点击已经上传的文件", file);
},
handleExceed(files, fileList) {
this.$message.warning(
`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
files.length + fileList.length
} 个文件`
);
},
beforeRemove(file, fileList) {
console.log("移除之前执行的函数", fileList);
return this.$confirm(`确定移除 ${file.name}?`);
},
handelSend() {
this.$refs.upload.submit();
},
},
};
</script>
<style>
</style>
手动上传只是手动触发一下上传的事件并没有特别的地方
在平常的开发中我们经常会有 上传图片的需求,然而仅仅使用element-ui上给我们的案例的话,并不能满足全部的开发场景(例如上传文件的同时还需要上传多个其他数据),所以我们就需要自定义上传方式,同时官方文档中也给了我们自定义的方式,但是我们这里的例子跟官方文档给我们的例子并不是完全一样
这里为了说明一些细节,我们就采用多文件上传,并上传其他参数,最终点击确定按钮的时候在上传文件的方式来说明问题
实现多文件上传的方式有两种,这两种方式大概都差不多,只不过有一种方式有一个细节需要我们注意一些
<template>
<div>
<el-upload
class="upload-demo"
ref="upload"
action=""
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:on-change="handelChange"
name="cover"
:multiple="true"
:auto-upload="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<div slot="tip" class="el-upload__tip">
只能上传jpg/png文件,且不超过500kb
</div>
</el-upload>
<el-button @click="handelSend">上传</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fileList: [],
};
},
methods: {
handelChange(file, fileList) {
this.fileList = fileList;
console.log("文件修改执行的函数", file, fileList);
},
handleRemove(file, fileList) {
console.log("移除文件执行的函数", file, fileList);
this.filesList = fileList;
},
handlePreview(file) {
console.log("点击已经上传的文件", file);
},
handleExceed(files, fileList) {
this.$message.warning(
`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
files.length + fileList.length
} 个文件`
);
},
beforeRemove(file, fileList) {
console.log("移除之前执行的函数", fileList);
return this.$confirm(`确定移除 ${file.name}?`);
},
handelSend() {
console.log("上传文件", this.fileList);
// 这里需要判断一下文件大小或者类型
// 自定义上传就需要我们使用fromdata对象来上传文件
let formdata = new FormData();
for (let i = 0; i < this.fileList.length; i++) {
// 我们上传的文件保存在每个文件对象的raw里边
formdata.append("file", this.fileList[i].raw);
}
// 添加其他属性
formdata.append("something", "something");
// 发送请求
},
},
};
</script>
<style>
</style>
利用官方提供的http-request函数自定义上传的话我们需要利用到官方给我们提供的手动上传的一个方式submit方法,同时我们还有一个特别注意的点就是http-request函数执行的次数,具体的我们看下边的代码
<template>
<div>
<el-upload
class="upload-demo"
ref="upload"
action=""
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:on-change="handelChange"
:http-request="uploadFile"
name="cover"
:multiple="true"
:auto-upload="false"
>
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<div slot="tip" class="el-upload__tip">
只能上传jpg/png文件,且不超过500kb
</div>
</el-upload>
<el-button @click="handelSend">上传</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fileList: [],
};
},
methods: {
handelChange(file, fileList) {
this.fileList = fileList;
console.log("文件修改执行的函数", file, fileList);
},
handleRemove(file, fileList) {
console.log("移除文件执行的函数", file, fileList);
this.filesList = fileList;
},
handlePreview(file) {
console.log("点击已经上传的文件", file);
},
handleExceed(files, fileList) {
this.$message.warning(
`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
files.length + fileList.length
} 个文件`
);
},
beforeRemove(file, fileList) {
console.log("移除之前执行的函数", fileList);
return this.$confirm(`确定移除 ${file.name}?`);
},
// 方法二
handelSend() {
let formdata = new FormData();
// 定义一个fromdata对象用于
this.formdata = formdata;
// 触发提交文件(执行submit()这个函数会根据有多少个文件就执行多少次http-requests所绑定的函数)
this.$refs.upload.submit();
this.formdata.append("something", "something");
// 发送请求,这里上传的的是this.fromdata而不是this.fileList
},
uploadFile(file) {
// 判断文件
console.log("自定义上传文件", file);
this.formdata.append("file", file.file);
},
},
};
</script>
<style>
</style>
需要注意的是我们自定义上传的方式并不会触发上传失败和上传成功的函数,所以需要我们自己根据上传情况手动调用
以上就是我上传文件的一些分享。另外给大家分享一下按需引入的小技巧,对于初学者的我们来说有时候按需引入并不知道引入什么,一般情况下我们引入的是官方文档中el后边的东西,然后我就发现官方文档的左边导航栏的字母正好就是我们按需引入的所要引入的东西(当然有时候我们不仅需要引入导航栏的字母也需要我们引入el后边的东西),而有些是通过this.$xxx的方式调用的,这篇博客也差不多包含了需要这种调用的全部东西了。
这周的分享就到这里了,下周继续努力吧!!!!
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢