前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue中上传文件_vue实现文件上传和下载

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

作者头像
全栈程序员站长
发布2022-09-27 16:10:41
1.1K0
发布2022-09-27 16:10:41
举报

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

例一:

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

代码语言:javascript
复制
<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框只展示文件名,不走接口,之后点击确定上传按钮统一上传

代码语言:javascript
复制
<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.cn/184365.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 上传文件同时携带选择form表单的其他内容
    • 例一:
      • 例二:
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档