vue form表单上传文件
<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表单上传文件的更多相关文章
- django 基于form表单上传文件和基于ajax上传文件
一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...
- 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType
回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...
- 使用form表单上传文件
在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...
- JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器
昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...
- PHP 后台程序配置config文件,及form表单上传文件
一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...
- nodejs 模拟form表单上传文件
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...
- form表单上传文件使用multipart请求处理
在开发Web应用程序时比较常见的功能之一,就是允许用户利用multipart请求将本地文件上传到服务器,而这正是Grails的坚固基石——spring MVC其中的一个优势.Spring通过对Serv ...
- 通过form表单上传文件获取后台传来的数据
小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...
- form表单上传文件
一.formData()直接获取form表单数据 例子:获取form表单的id给formData(),然后传给后台. 要求: 传入值的name值必须与后台接受的name相对应. form表单不能嵌套, ...
随机推荐
- js检测页面触底
<script> function getDocumentTop() { var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = ...
- Shell 编程 基础
本篇主要写一些shell脚本的基础知识,编程规范. 第一个shell脚本 [root@localhost ~]# vim first.sh #!/bin/bash # This is first Sh ...
- Hibernate各种状态(瞬时状态、持久化状态、游离|托管状态)之间的转换
- git命令中的--是什么意思?
转载自git命令中的--是什么意思? git命令中的--是什么意思? 看到个命令 git checkout -- files 不知道--代表什么.查了一下,--是linux的东西,用来标志命令项的结束 ...
- 编程题:SaturdayNightStay
"输入2019年的一个时间段,开始时间代表出发,结束时间代表在那一天返回,判断在该时间段内,如果旅行有多少个子时间段可以在周六晚上休息" * 周六晚上休息,即子时间段必须包含周六, ...
- 遗传算法介绍并附上Python代码
之前介绍过遗传算法,参见:https://www.cnblogs.com/LoganChen/p/7509702.html 我们用Python实现同样的问题解答. y=10*sin(5*x)+7*ab ...
- C# 只允许运行一个程序实例
using System; using System.Windows.Forms; using System.Runtime.InteropServices;//使用DllImport的必须. usi ...
- vsftp部署
安装 yum install -y vsftpd systemctl enable vsftpd.service systemctl start vsftpd.service systemctl st ...
- wraps装饰器的作用
装饰器的本质是一个闭包函数,作用在于不改变原函数功能和调用方法的基础上给它添加额外的功能.装饰器在装饰一个函数时,原函数就成了一个新的函数,也就是说其属性会发生变化,所以为了不改变原函数的属性,我们会 ...
- webpack.config.js配置信息的说明
module.exports = { entry: "./src/main.js", output: { filename: "build/build.js" ...