• 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[0]);
            },
            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 === 200) {
                        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);
                  }
                })
              }
            }
    复制代码
  • 相关阅读:
    Element-UI 表单验证规则rules 配置参数说明
    vue中 关于$emit的用法
    IEDA 自动生成类注释和方法注释
    maven配置多个镜像
    git 查看修改用户名,密码
    cnpm的安装(超级详细版)
    windows命令行中 启动应用程序
    通用遥控编码2262与1527解说
    Zeller Format
    字符串截取算法
  • 原文地址:https://www.cnblogs.com/rxbook/p/11966054.html
Copyright © 2020-2023  润新知