关于vue使用form上传文件
在vue中使用form表单上传文件文件的时候出现了一些问题,获取文件的时候一直返回null,
解决之后又出现发送到后台的file文件后台显示为空,解决源码
<template>
<div>
<Row>
<Col :sm="24" :md="24" lg:="24">
<form id="myForm" enctype="multipart/form-data" name="fileinfo" action="" target="uploadFrame"> //这里设置目标为iframe的name,这样提交表单数据的时候就不会刷新页面
<div class="img-file">
<label>请选择上传的文件:</label>
<input @change='changeImg' id='imgFile' type='file'/>
<!-- <input id="submit" type="submit" value="确认上传" /> -->
</div>
</form>
<iframe id="uploadFrame" name="uploadFrame" style="display:none;"></iframe> //解决form表单提交数据刷新问题
<!-- 展示服务器返回的图片 -->
<div class="box1">
<div class="imgbox">
<img class="auto-img" :src='srcs' :id='ids'/>
</div>
<div class="texts" :dates="dates">{{dates}}</div>
</div>
</Col>
</Row>
</div>
</template>
<script>
import Cookies from 'js-cookie';
import axios from 'axios';
import qs from 'qs';
export default{
data(){
return{
//默认图片地址,地址通过动态修改
srcs: 'http://test.laihaiqiang.com/image/timg.jpg',
// 图片id ,为了避免和标签的id冲突,尽量使用别称,如id1,ida等。
ids: 0,
// 创建时间
dates: '',
//文件
file: null,
}
},
methods:{
changeImg(){
var that = this; //这里使用一个变量 that来拼接this,这样的话在内部就使用thar,在外部就使用this,这样就不会出现指向丢失问题。
var file = document.getElementById("imgFile").files[0]; //获取文件
that.file = file; //获取data里面的预设默认file,并把获取到的文件赋给它
var ids = this.ids; //获取ata里面的id
var formData = new FormData(); //构造一个 FormData,把后台需要发送的参数添加到FormData里面,这样就可以直接把formData作为参数传递了
if (file) {
formData.append("access_token", Cookies.get('Admin-Token')); //接口需要传递的参数
formData.append("img", file); //接口需要传递的参数
formData.append("id", ids); //接口需要传递的参数
}
let potss = formData;
let urls = 'http://...........................'; //数据接口
axios.post(urls, potss)
.then(function(reh){
console.log('rehsdddddd===>>>', reh);
})
.catch(function(err){
console.log('err=ssss=>>', err);
})
},
},
created : function(){
var that = this;
console.log('1234567890987654321 ==> ', this.file);
let postToken = qs.stringify({
access_token : Cookies.get('Admin-Token'),
})
let api = 'http://.....................................................';
axios.post(api, postToken)
.then(function(res){
console.log('resss===>>>', res);
console.log('code===>>>', res.data.code);
console.log('message===>>>', res.data.message);
console.log('logo图片地址==>>', res.data.data.img_url);
console.log('logo图片id==>>', res.data.data.id);
console.log('logo图片创建日期==>>', res.data.data.create_time);
// 修改图片的路径
that.srcs = res.data.data.img_url;
//图片创建时间
if(res.data.data.create_time == '' || res.data.data.create_time == null){
that.dates = '';
}else{
that.dates = '添加时间:' + res.data.data.create_time;
}
//图片id
that.ids = res.data.data.id;
})
.catch(function(err){
console.log('err=ssss=>>', err);
})
}
}
</script>
<style scoped>
.imgbox{
width:200px;
height:200px;
margin:0 auto;
border: 1px solid #0f0;
}
.texts{
width:100%;
margin:10px 0 0;
text-align: center;
}
.upbox{
width:100%;
margin:0 auto;
text-align: center;
}
.auto-img{
display: block;
width:100%;
}
.upload{
margin:30px 0;
}
.box1{
width:100%;
}
</style>
关于vue使用form上传文件的更多相关文章
- c# 模拟表单提交,post form 上传文件、大数据内容
表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...
- c# 模拟表单提交,post form 上传文件、数据内容
转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...
- 通过form上传文件(php)
前段代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- form上传文件以及跨域异步上传
要设置了enctype属性才能上传,需要使用上传的jar包,这里使用的是cos-26Dec2008.jar, 而且后台获取值的时候,getfile要放在第一位 一次设置好上传格式后没有上传文件,也就没 ...
- 关于form 上传文件时的小问题
平时的form一般写成这样: <form action=" " method="" id="" name=""&g ...
- 使用vue+iview实现上传文件及常用的下载文件的方法
首先说明一下,我们这次主要用的还是iview的upload上传组件,下面直接上代码 <Upload ref="upload" multiple='true' //是否支持多文 ...
- iframe+form上传文件
<iframe id="iframe1" name="ifra1" style="display: none"></ifr ...
- VUE -- 用组件上传文件和用xmlrequest上传
xmlrequest: sendForm(str, types) { var form = this.$refs.ipas_form; var oOutput = document.querySele ...
- vue.js异步上传文件前后端代码
上传文件前端代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
随机推荐
- 双网卡+mitmproxy+iptables搭建SSL中间人(支持非HTTPS协议)
"想要解决一个问题,最根本方法的就是了解这一切是如何工作的,而不是玄学." --ASCII0X03 最近学习发现现在很多现成的安卓SSL中间人工具和教程都只针对HTTPS流量,比如 ...
- Python--Seaborn绘图和可视化--基本语法
博客地址:http://www.cnblogs.com/yudanqu/ Seaborn是对matplotlib的extend,是一个数据可视化库,提供更高级的API封装,在应用中更加的方便灵活.下面 ...
- Maven安装和使用
一.安 装 1.解压好后,添加系统环境变量 变量名:MAVEN_HOME 属性值:D:\apache-maven-3.3.3 //也就是解压的路径 path中添加:%MAVEN_HOME%\bi ...
- Qtp自动测试工具
QTP是基于GUI界面的自动化测试工具,用于系统的功能测试. QTP录制的是鼠标和键盘的消息.QTP录制回放时基于windows操作系统的消息机制.QTP在录制时监听应用程序的消息,监听到之后把消息放 ...
- RedHat Linux下iptables防火墙设置
一般情况下iptables已经包含在Linux发行版中.运行 # iptables --version 来查看系统是否安装iptables 启动iptables:# service iptables ...
- 计算器源码(数学式python)
''' ******************** 请计算表达式: 1 - 2 * ( (60-30 +(-40.0/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/1 ...
- Linux内存使用情况以及内存泄露分析之工具与方法
<Linux C/C++ Memory Leak Detection Tool> 1. 内存使用情况分析 1.1 系统总内存分析 通过cat /proc/meminfo,可用的物理内存=M ...
- Web测试——翻页功能测试用例
参考:https://wenku.baidu.com/view/e6462707de80d4d8d15a4f1e.html?rec_flag=default&mark_pay_doc=2&am ...
- spring-boot-starter-druid
i have found from the document, and seems that spring-boot only support tomcat-jdbc,HikariCP and DBC ...
- titanium环境配置
###### **工具:** > * [titanium studio](http://www.appcelerator.com/product/) > * [node.js](https ...