VUE element-ui 文件上传组件上传编码格式问题解决方法 multipart

项目需要element-ui上传文件,但是直接用upload的时候一直报错,没有设置编码格式,编码格式件就不是 multipart/form-data* 网上找了很多方法都用不起, 头痛!
直接设置好像也不行—>也有可能是自己太菜了.

但是现在

我终于找到了一个方法

安装axios

npm install axios
npm install --save axios vue-axios

import 'axios';
import 'vue-axios';

之后在页面上这样

    <el-upload
            style="display:inline-block"
            :limit="5"
            class="upload-demo"
            ref="upload"
            action="http://localhost:8080/upload"
            :file-list="fileList"
            :http-request="uploadSectionFile"
            :auto-upload="true"
            :before-remove="handleRemove"
        >
            <el-button slot="trigger" size="small" type="primary" plain>
                选取文件
            </el-button>
            <el-button
                style="margin-left: 10px;"
                size="small"
                icon="el-icon-upload2"
                type="success"
                @click="submitUpload"
            >
                导入
            </el-button>
        </el-upload>
import axios from 'axios';
export default {
    data() {
        return {
            fileList: []
        };
    },
    methods: {
        uploadSectionFile(param) {
            var fileObj = param.file;
            // FormData 对象
            var form = new FormData();
            // 文件对象
            alert('进来了');
            form.append('file', fileObj);
            axios({
                method: 'post',
                url: 'http://localhost:8080/upload',
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
                data: form
            }).then(res => {
                alert(res);
            });
        }
    },
    //删除时
    handleRemove(file, fileList) {
        var data = {
            attacMentCode: this.attachMentCode
        };
        deleteAttachment(data).then(res => {
            alert(res);
        });
    }
};
</script>

就能够完美解决了

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值