<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. python绘图 转

    Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...

  2. js 提取特定的时间区间段

    项目中遇到问题,需要根据用户的选择提取出一个时间的区间段,然后对后台进行请求. 基本思路,先根据new Date()对象求出start_time和end_time时间戳,然后把时间戳转化成new Da ...

  3. 【Spring Boot】Spring Boot之整合RabbitMQ并实现消息的发送和接收

    一.项目配置 1)引入maven坐标 <!--amqp--> <dependency> <groupId>org.springframework.boot</ ...

  4. G6 知识点

    Viser 一个基于 G2 实现的,为数据可视化工程师量身定制的工具. Viser-Graph 一个基于 G6 实现的,为呈现关系型数据的定制化工具. Mode 是 G6 提供的图上事件的管理机制. ...

  5. git 学习记录—— git 中的仓库、文件状态、修改和提交操作等

    最近开始学习使用版本控制工具  git .学习方式主要通过阅读 git 网站上的 Pro git 和动手实践,使用的系统为 Ubuntu16.04LTS,以及 Windows 8.1. 本文主要关注 ...

  6. win10 系统运行加速方法

    win10系统就是不太好用,很多功能我们硬件跟不上,会拖累系统运行速度,之前将win10优化了一部点,但是有些地方反而降低运行速度,因此需要关闭:1.磁盘的优化,这个说实话,可以自己来优化,没必要时刻 ...

  7. list数组排序 Collections 按Date时间降序排列

    @ResponseBody @RequestMapping(value = {"K12", "12"}) public String refurbishLigh ...

  8. The Secret Life of Types in Swift

    At first glance, Swift looked different as a language compared to Objective-C because of the modern ...

  9. linux内核中的文件描述符(二)--socket和文件描述符

    http://blog.csdn.net/ce123_zhouwei/article/details/8459730 Linux内核中的文件描述符(二)--socket和文件描述符 Kernel ve ...

  10. matlab基础向1-6:基础语法

    1.软件中如何运行代码? 命令行直接写代码,回车执行,也可以在文件里编写代码,比如有文件hello.m,点击“Run”直接运行或者在命令行窗口里输入“hello+回车”运行. 2.清空命令行 clc+ ...