栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Vue上传文件到springboot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vue上传文件到springboot

            
                点击上传
                
只能上传jpg/png文件,且不超过500kb

上传到服务器

在data中定义

        data: {
            fileLists: [],

使用on-change属性,将每次添加的文件依次推入到fileLists中,然后将fileLists的文件使用formData封装,请求头为'multipart/form-data'

            handleChange(file) {
                this.fileLists.push(file)
            },
            submitUpload() {
                const formData = new FormData()
                // 因为要传一个文件数组过去,所以要循环append
                this.fileLists.forEach((file) => {
                    formData.append('files', file.raw)
                })
                this.uploadRequest(formData)
            },
            uploadRequest(formData) {
                let that = this;
                axios.post("http://localhost:8080/new/upload", formData, {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                })
                    .then(function (res) {
                        console.log(res)
                        if (res.data.result == "ok") {
                            that.$message.success('上传成功!');
                        }
                    })
                    .catch(function (err) {
                        that.$message.error('网络请求异常!');
                    })
            },

后端代码:

    @PostMapping("/new/upload")
    public baseResultModel uploadPic(@RequestParam("files") MultipartFile[] files) {
	// 逻辑
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777937.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号