vue中上传文件_vue实现文件上传和下载

vue中上传文件_vue实现文件上传和下载列子一:简单的上传文件,先把文件上传到input框只展示文件名,不走接口,之后点击确定上传按钮统一上传<template> <div> <Col> <FormItemlabel=”上传文件:”prop=”plugin_name”> <Inputv-model=”setValidate.plugin_name”placeholder=”请选择上传文件(.ZIP格式)”></Input> </Fo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

上传文件同时携带选择form表单的其他内容

例一:

接口需要传文件以及其他内容的参数,这里需要formdata封装再提交数据

<template>
	<FormItem label="文件上传:" class="objBox" prop="object">
		<Upload
			:before-upload="handleUpload"
			action='/url'
			type="drag"
			style="width:520px;height:120px"
			v-model="setValidate.file"
		>
			<div style="padding:10px 0">
				<Icon type="ios-cloud-upload" size="52" style="#3399ff"></Icon>
				<p style="margin-top:10px;font-size:14px">点击或拖拽文件至此即可上传文件</p>
				<p style="margin-top:20px;font-size:14px;color:red">请上传10GB以内的待测对象,支持.zip格式</p>
			</div>
		</Upload>
		<div style="margin-top:20px;width:360px" v-if="setValidate.file !=null">
			文件:{ 
   { 
   setValidate.file.name}}
			<span style="color:#2d8cf0;float:right" @click="removeFile">移除</span>
		</div>
	</FormItem>
	<Spin>正在执行,请稍等...</Spin>
	<Row style="margin:0 auto">
		<Col span="8" offset="8">
			<Button @click="Taskadd('setValidate')" type="primary">创建</Button>
			<Button @click="back()">取消</Button>
		</Col>
	</Row>
</template>
<script>
	export default { 
   
		data (){ 
   
			setValidate:{ 
   
				file:'',
				taskId:'',
				taskName:''
			}
		},
		methods:{ 
   
			Taskadd(){ 
   
				let _this = this;
				if(!setValidate.file){ 
   
					_this.$Message.error("请上传文件")
					return false
				}
				//这里是用了iview里面的form表单验证
				_this.$refs['setValidate'].Validate(vaild)=>{ 
   
					if(valid){ 
   
						let formData = new FormData()
						//通过append追加数据
						formData.append('file',_this.setValidate.file)
						formData.append('taskId',_this.setValidate.taskId)
						formData.append('taskName',_this.setValidate.taskName)
						formData.append('userName',_this.$Global.getCookie('userName'))
						_this.axios({ 
   
							method:'post',
							url:'/url',
							headers:{ 
   'Content-Type':'multipart/form-data'},
							data:formData
						}).then( (res)=>{ 
   
							if(res.result == 'SUCCESS'){ 
   
								_this.$Message.info("上传成功!")
							}
						})
					}else{ 
   
						_this.$Message.error("表单验证失败!")
					}
				}
			},
			removeFile(){ 
   
				this.setValidate.file = null;
			},
			back(){ 
   
				this.$router.push({ 
   path:'/XXXX'})
			}
		}
	}
</script>

例二:

简单的上传文件,先把文件上传到input框只展示文件名,不走接口,之后点击确定上传按钮统一上传

<template>
	<div>
		<Col>
			<FormItem label="上传文件:" prop="plugin_name">
				<Input v-model="setValidate.plugin_name" placeholder="请选择上传文件(.ZIP格式)"></Input>
			</FormItem>
		</Col>
		<Col>
			<Upload
				align="left"
				name="file"
				:data="'/url?taskid=' + taskid"
				:format="['zip']"
				:befor-upload="handleUpload"
				:on-success="uploadSuccess"
				:on-format-error="handleFormatError"
				:on-error="uploadleError"
				:show-upload-list="false"
				v-model="setValidate.plugin_name">
				<Button icon="ios-cloud-upload-outline">上传文件</Button>
			</Upload>
		</Col>
		<Col>
			<Button type="primary" @click="updown('setValidate')">确定上传</Button>
		</Col>
	</div>
</template>
<script>
	export default { 
   
		data (){ 
   
			return { 
   
				loading:false,
				setValidate:{ 
   
					plugin_name:'',
				}
			}
		},
		methods:{ 
   
			//导入之前
			handleUpload(file){ 
   
				let _this = this;
				_this.setValdate.plugin_name = file.name;
				_this.file = file;
				return false
			},
			//导入成功
			uploadSuccess(res,file){ 
   
				let _this = this;
				_this.result = res.result;
				if(res.result == 'file'){ 
   
					_this.$Message.info(res.err_desc);
				}else if(res.result == 'SUCCESS'){ 
   
					_this.$Message.info("文件上传成功!")
					_this.loading = false;
					_this.file = null;
				}else { 
   
					_this.$Message.info("文件上传失败!")
				}
			},
			//文件格式验证失败
			handleFormatError(file){ 
   
				this.$Message.error(file.name + '文件格式不正确,请上传正确的格式文件!');
			},
			uploadleError(res,file){ 
   
				let _this = this;
				_this.error = res.result;
				_this.$Message.error("文件上传失败,请重新上传!")
			},
			updown(){ 
   
				let _this = this;
				let file1 = _this.file;
				_this.$refs['setValidate'].validate((valid)=>{ 
   
					if(valid){ 
   
						_this.$refs.upload.post(file1)
					}else{ 
   
						_this.$Message.error('表单验证失败!')
					}
				})
			}
		}
	}
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/184365.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • shell循环读取文件拼接字符串

    shell循环读取文件拼接字符串1注意通道和重定向的区别通道会开启子shell,于是通道语句块内的变量修改是无法影响到其外的变量的,故使用重定向;2windows文件末尾是/r/n拼接来自windows系统的文件务必首先注意:[nash5camFiles]#cat-AcamFiles.ori0.863631-0.269646-0.425949^M$

    2022年7月24日
    7
  • MongoDB(三)mongoDB下载和安装[通俗易懂]

    MongoDB(三)mongoDB下载和安装

    2022年1月27日
    59
  • Nginx配置使用详解

    Nginx配置使用详解详细讲解Nginx的配置文件以及使用

    2022年7月14日
    24
  • java中method方法_java修改字体大小

    java中method方法_java修改字体大小LocalDateaThousandDaysLater=hello.plusDays(1000);这个调用后hello会有什么变化?他会改为1000天之后的日期吗?事实上,并没有。plusDays()方法会生成一个新的LocalDate对象,然后将这个新对象赋值给aThousandDaysLater。原来的对象在堆中不会有任何改变。我们说的plusDays方法没有更改调用plusDays方法的

    2025年9月12日
    6
  • JAVA HD japan_电竞小金刚:优派VX2778-hd-PRO

    JAVA HD japan_电竞小金刚:优派VX2778-hd-PRO144Hz一直是游戏爱好者追捧的香饽饽,再配上色彩还原能力极为优秀的IPS面板,那一定是无数玩家心中的梦想。随着面板技术越来越成熟,144Hz的IPS屏的价格渐渐走下神坛,有不少入门级电竞显示器开始配备这种的面板。开箱今天给各位开箱的优派VX2778-hd-PRO就是这样一款产品。左下角IPS144Hz的标识格外显眼。优派的支架是我一直非常喜欢的,…

    2022年7月8日
    56
  • 小树311_森林小道

    小树311_森林小道原题链接森森开了一家快递公司,叫森森快递。因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(N−1)编号。由于道路限制,第i号城市(i=0,⋯,N−2)与第(i+1)号城市中间往返的运输货物重量在同一时刻不能超过C​i​​ 公斤。公司开张后很快接到了Q张订单,其中j张订单描述了某些指定的货物要从S​j​​ 号城市运输到T​j​​ 号城市。这里我们简单地假设所有货物都有无限货源,森森会不定时地挑选其中一部分货物进行运输。安全起见,这些货物不会在中

    2022年8月9日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号