• vue上传文件


    1.表单提交文件

    html

    <template>
        <div>
            <el-button @click="getFile" style="margin-top: 10px">
                <i class="el-icon-upload">i> 上传
            el-button>
            <input type="file" ref="file" style="display: none;" v-on:change="handleFileUpload($event)">
        div>
    template>
    
    <script>
    // import postUpload from '../../api/index.js'
    export default {
        methods: {
            // 打开文件
            getFile () {
                this.$refs.file.click()
            },
            // 获取文件
            handleFileUpload (event) {
                // 阻止发生默认行为
                event.preventDefault();
                let formData = new FormData()
                let file = this.$refs.file.files[0]
                formData.append('file', file)
                // console.log(formData.get('file'))
                this.onUpload(formData)
            },
            // 上传文件
            onUpload (formData) {
                postUpload(formData).then((res) => {
                    this.mdl.pic = res.result.uri
                    this.$message.success(this.$t('UPLOAD_SUCCESS'))
                }).catch((e) => {
                    this.$message.error(e.message)
                })
            },
        }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    index.js

    axios请求:

    axios请求都是封装好的,重点就是需要加请求头: headers:{ ‘Content-Type’: ‘multipart/form-data’ }

    // 文件上传
    export function postUpload (file) {
        return axios({
            url: 'upload',
            method: 'post',
            data: file,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.Elementui封装的组件提交文件

    html

    点击上传
    • 1

    index.js

    axios请求:

    // 文件上传
    export function postUpload (file) {
        return axios({
            url: 'upload',
            method: 'post',
            data: file,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    转载

    博主

    推荐

    博主1
    博主2
    博主3
    博主4

    最后

    感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

  • 相关阅读:
    Windows操作系统基础-第03课-DNS服务介绍
    cesium态势标会(面积测量 ---- 不可修改)
    java数据结构与算法刷题-----LeetCode1109:航班预订统计
    AI服务器,深度学习英特尔服务器主板和超微服务器主板哪个牌子好?
    java计算机毕业设计高校教学资源共享平台MyBatis+系统+LW文档+源码+调试部署
    API与C++中的API
    [面试直通版]操作系统之编程语言与运行原理(下)
    centos的docker镜像下载ffmpeg的方式
    flink1.15.0消费kafka 报错 The coordinator is not available.
    MasaFramework -- 更优雅的获取配置信息
  • 原文地址:https://blog.csdn.net/m0_49714202/article/details/126097106