首先在项目里用了拦截器的,由于拦截器会将传递的参数转成对象,所以你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. redis学习(八)——redis应用场景

    毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...

  2. C#订阅与发布标准实现

    大概看了下C#官方提供的IObservable接口以及IObserver接口来实现发布和订阅,写的很标准,很有代表性,做下笔记,以后要是项目需要用到发布订阅再基于自己的需求改: public clas ...

  3. Android Studio 常用快捷键及常用设置

    Android Studio 常用快捷键及常用设置 一.常用快捷键 快捷键 描述 Ctrl + Alt + L 格式化代码 Ctrl + ( +/- ) 展开/折叠 代码块 Ctrl + Shift ...

  4. angular.injector()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. __x__(4)0905第二天__软件架构

    软件架构 C/S 架构,客户端/服务器,用户通过客户端使用软件. 一般的应用软件都是 C/S 架构,如 QQ,360 等等. C 为 Client,用户电脑使用的软件. S 为 Server,服务器, ...

  6. ElasticSeaarch 遇到的问题 (-)

    1 elasticSearch 不能通过ip访问 智只能通过localhost访问,或者在外网部署的时候不能访问: elasticsearch.yml文件 中将下面的配置去掉注释符,  network ...

  7. 下面findmax函数将计算数组中的最大元素及其下标值,请编写该函数。

    #include <stdio.h> void findmax ( int s[ ], int t, int *k ) { int i; *k = ; ; i < t; i++) { ...

  8. 三类设计模式UML图

    http://design-patterns.readthedocs.org/zh_CN/latest/index.html

  9. 2.2String工具类

    1:split方法 public class SplitDemo1 { public static String[] name = new String[20]; public SplitDemo1( ...

  10. centos7 部署vnc

    不做过多介绍了,下面直接记录下centos7系统下安装配置vncserver的操作记录 0)更改为启动桌面或命令行模式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...