首先在项目里用了拦截器的,由于拦截器会将传递的参数转成对象,所以你i提交的时候会发现multipart/form-data或转变成application/json

其次关于input的文件上传是需要一个非常纯净的axios的,于是我就在main.js里重新挂载了一个axios

//main.js
//自定义
var instance = axios.create({
baseURL:'',
timeout:5000,
headers:{"Content-Type":"multipart/form-data"}
}); Vue.prototype.axios = axios;
Vue.prototype.instance=instance;

在组件中上传代码

var img_file = this.$refs.inputs;
var formData = new FormData(img_file);
var fileObj = img_file.files[0];
console.log(formData)
formData.append("dsc","dsc");
formData.append("file",fileObj);
console.log(fileObj)
this.instance.post(url,formData).then((res)=>{
this.getInit()
})

就这样就可以正常上传文件

在这里顺便也记一下下载后端返回的文件资源

axios.get(url, {
responseType: 'blob', //重要
}).then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
let fname = 'report.pdf';
link.href = url;
link.setAttribute('download', fname);
document.body.appendChild(link);
link.click();
})

url为后台返回的资源地址,这里会报跨域的错,找后台设置一下就好了

注:测试的时候发现在火狐浏览器上会报错 Argument 1 of FormData.constructor does not implement interface HTMLFormEle

自需要做如下更改就好

//var img_file = this.$refs.inputs;
//var evt=window.event || el;
//console.log(evt)
var img_file = evt.target.files[0];
console.log(img_file)
var formData = new FormData(document.getElementById('uploadForm')[0]);
//var fileObj = img_file.files[0];
var fileObj = img_file
console.log(formData)
formData.append("dsc","dsc");
formData.append("file",fileObj);
console.log(fileObj)
this.instance.post(url,formData).then((res)=>{
this.getInit()
})

vue中上传文件之multipart/form-data的更多相关文章

  1. vue中上传文件相同文件名没反应

    vue项目中会遇到上传文件的需求,jquery会有一些插件很方便,如果不使用插件网上的方法没有太容易的而且很多是原生JS或者基于jQuery操作dom结构的.那么在vue项目中如何实现呢,还有如何模拟 ...

  2. vue 上传文件 和 下载文件

    Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...

  3. vue 上传文件 和 下载文件 面试的时候被问到过

    Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...

  4. 使用curl 上传文件,multipart/form-data

    使用curl 上传文件,multipart/form-data 1. 不使用-F,curl内置multipart/form-data功能: 2. 文件内容与真实数据无关,用abc代替数据,依然可以上传 ...

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

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

  6. axios+Vue上传文件显示进度

    一,前言 最近在用Vue,然后上传文件时需要显示进度,于是网上搜了一下,经过自己实测终于也弄明白了 二,效果 三,代码 HTML代码 <div id="app"> &l ...

  7. element-ui中上传文件upload

    <el-upload class="upload-demo" name="targetFile" ref="upload" :with ...

  8. Vue上传文件:ElementUI中的upload实现

    一.上传文件实现 两种实现方式: 1.直接action <el-upload  .利用before-upload属性 此种方式有个弊端,就是action是必选的参数,那么action如果和pos ...

  9. vue上传文件

    <div> <input type="file" class="file" name="file" @change=&qu ...

随机推荐

  1. python 几个简单算法详解

    一.冒泡排序 基本思想:它的思路很有特点循环,两两向后比较.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数 ...

  2. rhel配置网络yum源

    redhat 7.2配置网易yum源 卸载原来的yum源和安装必须的rpm包 1)确保虚拟机能够连接外网 2)前往http://mirrors.163.com/centos/7/os/x86_64/P ...

  3. MyBatis3系列__01HelloWorld

    # MyBatis作为一个ORM框架,其重要程度不用过多介绍.下面开始一起学习吧:本博客的编程方法与MyBatis官方文档基本一致:## 1.创建一个数据库mybatis_learn以及对应的表tbl ...

  4. 爬虫——scrapy入门

    scrapy 安装scrapy pip install scrapy windows可能安装失败,需要先安装c++库或twisted,pip install twisted 创建项目 scrapy s ...

  5. 项目导入之后报错:The import javax.servlet cannot be resolved

    项目导入之后报错:The import javax.servlet cannot be resolved 解决方法:在Eclipse中,右击项目,选择Build Path->configure ...

  6. 马昕璐201771010118《面向对象程序设计(java)》第七周学习总结

    第一部分:理论知识学习部分 Java用于控制可见性的4个访问权限修饰符: 1.private(只有该类可以访问) 2.protected(该类及其子类的成员可以访问,同一个包中的类也可访问) 3.pu ...

  7. react_app 项目开发 (2)_axios_pubsub-js

    生产环境打包并运行 yarn run build 会src代码进行打包处理,在内存中生成打包文件 将打包文件保存至内存 yarn global add serve serve -s build 将 b ...

  8. linux学习:find用法整理

    find path -option [ -print ] [ -exec -ok command ] {} \; path: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录 ...

  9. amazeui表单form异步提交方法

    // 留言提交 $(function() { var $form = $('#ue-form'); $form.validator({ validateOnSubmit: true, submit: ...

  10. 螺旋矩阵 java实现(待消化)

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/4 17:13 * @description ...