在vue中使用form表单上传文件文件的时候出现了一些问题,获取文件的时候一直返回null,

解决之后又出现发送到后台的file文件后台显示为空,解决源码

<template>
  <div>
    <Row>
      <Col :sm="24" :md="24" lg:="24">

        <form id="myForm" enctype="multipart/form-data" name="fileinfo" action="" target="uploadFrame">  //这里设置目标为iframe的name,这样提交表单数据的时候就不会刷新页面
          <div class="img-file">
            <label>请选择上传的文件:</label>
            <input @change='changeImg' id='imgFile' type='file'/>
            <!-- <input id="submit" type="submit" value="确认上传" /> -->
          </div>
        </form>
        <iframe id="uploadFrame" name="uploadFrame" style="display:none;"></iframe>  //解决form表单提交数据刷新问题

        <!-- 展示服务器返回的图片 -->
        <div class="box1">
          <div class="imgbox">
            <img class="auto-img" :src='srcs' :id='ids'/>
          </div>
          <div class="texts" :dates="dates">{{dates}}</div>
        </div>
      </Col>
    </Row>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import axios from 'axios';
  import qs from 'qs';
  export default{
    data(){
      return{
        //默认图片地址,地址通过动态修改
        srcs: 'http://test.laihaiqiang.com/image/timg.jpg', 
        // 图片id ,为了避免和标签的id冲突,尽量使用别称,如id1,ida等。
        ids: 0,
        // 创建时间
        dates: '',

        //文件
        file: null,
      }
    },
    methods:{
      changeImg(){
        var that = this;  //这里使用一个变量 that来拼接this,这样的话在内部就使用thar,在外部就使用this,这样就不会出现指向丢失问题。
        var file = document.getElementById("imgFile").files[0]; //获取文件
        that.file = file;  //获取data里面的预设默认file,并把获取到的文件赋给它 
        var ids = this.ids; //获取ata里面的id

        var formData = new FormData(); //构造一个 FormData,把后台需要发送的参数添加到FormData里面,这样就可以直接把formData作为参数传递了
        if (file) {
          formData.append("access_token", Cookies.get('Admin-Token')); //接口需要传递的参数
          formData.append("img", file);   //接口需要传递的参数
          formData.append("id", ids);    //接口需要传递的参数
        }

        let potss = formData;
        let urls = 'http://...........................';   //数据接口
        axios.post(urls, potss)
        .then(function(reh){
          console.log('rehsdddddd===>>>', reh);
        })
        .catch(function(err){
          console.log('err=ssss=>>', err);
        })
      },
    },
    created : function(){
      var that = this;
      console.log('1234567890987654321 ==> ', this.file);
      let postToken = qs.stringify({
       access_token : Cookies.get('Admin-Token'),
      })
      let api = 'http://.....................................................';
      axios.post(api, postToken)
      .then(function(res){
        console.log('resss===>>>', res);
        console.log('code===>>>', res.data.code);
        console.log('message===>>>', res.data.message);
        console.log('logo图片地址==>>', res.data.data.img_url);
        console.log('logo图片id==>>', res.data.data.id);
        console.log('logo图片创建日期==>>', res.data.data.create_time);

        // 修改图片的路径
        that.srcs = res.data.data.img_url;
        //图片创建时间
        if(res.data.data.create_time == '' || res.data.data.create_time == null){
          that.dates = '';
        }else{
          that.dates = '添加时间:' + res.data.data.create_time;
        }
        //图片id
        that.ids = res.data.data.id;

      })
      .catch(function(err){
        console.log('err=ssss=>>', err);
      })
    }
  }
</script>

<style scoped>
  .imgbox{
    width:200px;
    height:200px;
    margin:0 auto;
    border: 1px solid #0f0;
  }
  .texts{
    width:100%;
    margin:10px 0 0;
    text-align: center;
  }
  .upbox{
    width:100%;
    margin:0 auto;
    text-align: center;
  }
  .auto-img{
    display: block;
    width:100%;
  }
  .upload{
    margin:30px 0;
  }
  .box1{
    width:100%;
  }
</style>

关于vue使用form上传文件的更多相关文章

  1. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

  2. c# 模拟表单提交,post form 上传文件、数据内容

    转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...

  3. 通过form上传文件(php)

    前段代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...

  4. form上传文件以及跨域异步上传

    要设置了enctype属性才能上传,需要使用上传的jar包,这里使用的是cos-26Dec2008.jar, 而且后台获取值的时候,getfile要放在第一位 一次设置好上传格式后没有上传文件,也就没 ...

  5. 关于form 上传文件时的小问题

    平时的form一般写成这样: <form action=" " method="" id="" name=""&g ...

  6. 使用vue+iview实现上传文件及常用的下载文件的方法

    首先说明一下,我们这次主要用的还是iview的upload上传组件,下面直接上代码 <Upload ref="upload" multiple='true' //是否支持多文 ...

  7. iframe+form上传文件

    <iframe id="iframe1" name="ifra1" style="display: none"></ifr ...

  8. VUE -- 用组件上传文件和用xmlrequest上传

    xmlrequest: sendForm(str, types) { var form = this.$refs.ipas_form; var oOutput = document.querySele ...

  9. vue.js异步上传文件前后端代码

    上传文件前端代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...

随机推荐

  1. 设计模式的征途—22.中介者(Mediator)模式

    我们都用过QQ,它有两种聊天方式:一是私聊,二是群聊.使用QQ群,一个用户就可以向多个用户发送相同的信息和文件,从而无需一一发送,节省大量时间.通过引入群的机制,极大地减少系统中用户之间的两两通信,用 ...

  2. 树莓派配置watchdog

    安装watchdog apt install watchdog 编辑/etc/modules,添加bcm2708_wdog 编辑/etc/watchdog.conf watchdog-device = ...

  3. python笔试题(1)

            为了充实自己,小编决定上传自己见到的笔试题和面试题.可能要写好长时间,一时半会写不了多少,只能说遇到多少写多少吧,但是只要小编有时间,会持续上传(但是答案却不能保证,所以有看到错误的及 ...

  4. arcEngine开发之IMap、ILayer、IFeatureLayer和IFeatureClass关系

    刚开时学习 Engine 开发时,对于这几个接口之间的关系总是理不清,因此写下这篇文章做个总结. 是什么 在 engine 开发中,我觉得使用过程中应该将每个接口对应到 ArcMap 中的具体事物中, ...

  5. PAT1013: Battle Over Cities

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  6. java位 、字节 、字符的梳理

    1字节(byte)=8位(bit) char=2字节(这是因为char是Java中的保留字,Java用的是Unicode,所以char在Java中是16位即2个字节的.) 附: String str= ...

  7. Python报错:SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 6, but no encoding declared...

    本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=686 具体报错内容: File "1.py", line 6 SyntaxError: Non- ...

  8. 浅谈服务间通信【MQ在分布式系统中的使用场景】

    解决的问题 一项技术的产生必然是为了解决问题而生,了解了一项技术解决的问题,就能够很轻松的理解这项技术的设计根本,从而更好地理解与使用这项技术. 消息中间件和RPC从根本上来说都是为了解决分布式系统的 ...

  9. Java 字符流文件读写

    上篇文章,我们介绍了 Java 的文件字节流框架中的相关内容,而我们本篇文章将着重于文件字符流的相关内容. 首先需要明确一点的是,字节流处理文件的时候是基于字节的,而字符流处理文件则是基于一个个字符为 ...

  10. 他学习一年Python找不到工作,大佬都说你别再学Python了!

    引言: 都说,滴水穿石非一日之功.然而有些人即使奋斗一辈子也比不上别人一年,别人学习一年比不得你学习一个月.其中缘由,有些人看了大半辈子还没看明白. 即使Python这么火,为何你学习一年的Pytho ...