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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm激活码key is invalid(JetBrains全家桶)

    (pycharm激活码key is invalid)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月31日
    663
  • leetcode-103二叉树的锯齿形层序遍历「建议收藏」

    leetcode-103二叉树的锯齿形层序遍历「建议收藏」给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回锯齿形层序遍历如下:[ [3], [20,9], [15,7]]/** * Definition for a binary tree node. * struct TreeNode { * int

    2022年8月8日
    4
  • C++之constexpr详解

    C++之constexpr详解constexpr表达式是指值不会改变并且在编译过程就能得到计算结果的表达式。声明为constexpr的变量一定是一个const变量,而且必须用常量表达式初始化:constexprintmf=20;//20是常量表达式constexprintlimit=mf+1;//mf+1是常量表达式constexprintsz=size();//之后当siz…

    2025年8月6日
    2
  • 免费好用的cdn_如何查询cdn提供商

    免费好用的cdn_如何查询cdn提供商CDN介绍CDN也称内容分发网络,其原理大概是将服务内容分发至全网加速节点,让用户从就近的服务器节点上获取内容,从而提高网站的访问速度。大部分服务商(如阿里云,网易蜂巢,京东云等)的CDN服务是按使用量收费的,也有一些服务商提供免费的CDN服务,本文简单的总结一下目前可免费使用的CDN,对个人网站来说,免费的已经够用了。腾讯云CDN腾讯云CDN官网:https://cloud.tencent.com/product/cdn腾讯云可以免费申请SSL证书,腾讯云CDN也能很好的支持SSL证书,从而实

    2022年9月10日
    3
  • 新一代金融神话Filecoin[通俗易懂]

    新一代金融神话Filecoin[通俗易懂]Filecoin是区块链史上耗时最短的ICO融资项目,受到了DCG集团、文克莱沃斯兄弟基金会、联合广场风投、AndersonHollotz基金和红杉资本等投资巨头的青睐。储存即收益,Filecoin与BTC的工作证明是不同的。filecoin只需要提供存储空间和宽带,就可以满足获取身份证明的需求。因此,Filecoin可以从根本上提高人类的效率,是一种真正的共享经济,它可以极大地推动存储资源的使用方式。由于Filecoin是基于强大的IPFS协议,并且由于IPFS的大量应用,Filecoin作为IPFS

    2022年9月12日
    2
  • Java基础语法(五)运算符的那些事

    Java基础语法(五)运算符的那些事

    2021年4月21日
    185

发表回复

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

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