<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
<div id="app" v-cloak>
<input type="text" v-model="param.title">
<input type="text" v-model="param.content">
<input type="file" @change="getFile($event,'file_avatar')">
<input type="file" @change="getFile($event,'file_thumb')">
<button @click="submitForm($event)">OK</button>
</div> <script>
new Vue({
el: '#app',
data: {
param: {
title: info.title,
content: info.content,
file_avatar: '',
file_thumb: '',
},
formData: new FormData(), },
mounted: function () { },
methods: {
getFile(event, input_file_name) {
this.formData.append(input_file_name, event.target.files[]);
},
submitForm(event) {
event.preventDefault();
for (let i in this.param) {
this.formData.append(i, this.param[i]);
}
let config = {
headers: {
'Content-Type': 'multipart/form-data'
}
};
this.$http.post('/url', this.formData, config).then(function (res) {
if (res.status === ) {
console.log(res);
}
}).catch((error) => {
console.log(error);
});
}
}, });
</script>
 

单独上传文件:

<input class="file" name="file" type="file" accept="image/png,image/gif,image/jpeg" @change="update"/>
methods: {
update(e){
let file = e.target.files[0];
let param = new FormData(); //创建form对象
param.append('file',file);//通过append向form对象添加数据
console.log(param.get('file')); //FormData私有类对象,访问不到,可以通过get判断值是否传进去
let config = {
headers:{'Content-Type':'multipart/form-data'}
}; //添加请求头
this.$http.post('http://127.0.0.1:8081/upload',param,config)
.then(response=>{
console.log(response.data);
})
}
}

Form表单上传文件:

<form>
<input type="text" value="" v-model="name" placeholder="请输入用户名">
<input type="text" value="" v-model="age" placeholder="请输入年龄">
<input type="file" @change="getFile($event)">
<button @click="submitForm($event)">提交</button>
</form>
    data: {
name: '',
age: '',
file: ''
},
methods: {
getFile(event) {
this.file = event.target.files[0];
console.log(this.file);
},
submitForm(event) {
event.preventDefault();
let formData = new FormData();
formData.append('name', this.name);
formData.append('age', this.age);
formData.append('file', this.file); let config = {
headers: {
'Content-Type': 'multipart/form-data'
}
} this.$http.post('http://127.0.0.1:8081/upload', formData, config).then(function (response) {
if (response.status === 200) {
console.log(response.data);
}
})
}
}

vue form表单上传文件的更多相关文章

  1. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  2. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  3. 使用form表单上传文件

    在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...

  4. JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器

    昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...

  5. PHP 后台程序配置config文件,及form表单上传文件

    一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...

  6. nodejs 模拟form表单上传文件

    使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...

  7. form表单上传文件使用multipart请求处理

    在开发Web应用程序时比较常见的功能之一,就是允许用户利用multipart请求将本地文件上传到服务器,而这正是Grails的坚固基石——spring MVC其中的一个优势.Spring通过对Serv ...

  8. 通过form表单上传文件获取后台传来的数据

    小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...

  9. form表单上传文件

    一.formData()直接获取form表单数据 例子:获取form表单的id给formData(),然后传给后台. 要求: 传入值的name值必须与后台接受的name相对应. form表单不能嵌套, ...

随机推荐

  1. nginx与php分离搭建

    nginx与php分离搭建 nginx配置 server { listen       80;      定义端口 server_name  www.a.org;    定义域名 location / ...

  2. P1486 [NOI2004]郁闷的出纳员[权值线段树]

    权值线段树. 我们只用维护一个人是否存在,以及他当前排名,而不关心工资的具体值,这个可以直接算. 不难发现,如果不考虑新的员工,所有员工的工资的差值是不变的. 而加进来一个新的员工时,其工资为\(x\ ...

  3. python案例-判断素数

    from math import sqrt def is_prime(num): for rea in range(2,int(sqrt(num)+1)): if num%rea==0: return ...

  4. Gym - 101848D:XOR(线性基 欧拉降幂)

    题意:给定N,K,P,表示现在有一个集合{0, 1, ..., 2n - 1},问有多少个非空子集的异或和为K: 答案%P.(1 ≤ n ≤ 1018, 0 ≤ k ≤ min(2n - 1, 101 ...

  5. libimobiledevice的安装与使用

    说明:libimobiledevice相当于安卓的adb,可以使用命令对ios应用进行安装卸载等操作. 在MacOS下安装libimobiledevice: $ brew uninstall idev ...

  6. MapReduce的初识

    MapReduce是什么 HDFS:分布式存储系统 MapReduce:分布式计算系统 YARN:hadoop 的资源调度系统 Common:以上三大组件的底层支撑组件,主要提供基础工具包和 RPC ...

  7. Word中同样行间距,同样字号,同样字体,但是肉眼看起来行距不一样

    感谢博主转载:https://blog.csdn.net/hongweigg/article/details/47130009 困扰了我好久,直接上解决办法: 然后选择 自定义页边距 选择 无网络(N ...

  8. Ansible自动部署tomcat

    1.首先准备3台机器 ansible机器:192.168.52.34 目标主机:192.168.52.35 目标主机:192.168.52.36 2.关闭防火墙 [root@localhost ~]# ...

  9. python 日期、时间、字符串相互转换(转载)

    原文链接:https://www.cnblogs.com/huhu-xiaomaomi/p/10338472.html 在python中,日期类型date和日期时间类型dateTime是不能比较的. ...

  10. 用户和用户组管理——passwd、shadow、group、gshadow

    在Linux操作系统中,任何一个文件都归属于某一个特定的用户,而任何一个用户都归属于至少一个用户组. 1.用账号文件——passwd 该文件在/etc/passwd 目录下,是保证系统安全的关键文件. ...