首页 > 解决方案 > 如何将照片上传到服务器

问题描述

我正在尝试将照片从我的 Vue 前端上传到服务器。

 const fd = new FormData();
                fd.append('image', this.selectedFile)

                console.log(fd);
                axios.post(`/api/users/46/set-avatar`, fd)
                    .then(resp => {
                        console.log(resp);
                    })
                    .catch(e => console.log(e));

this.selectedFile = event.target.files[0];上传图像的值在哪里(通过 input type="file")。发送此请求我收到 400 错误。我不太确定问题出在哪里,我已经尝试发送 this.selectedFile,但它仍然无法正常工作。

我们在项目中使用 swagger,它说:

curl -X POST "http://localhost:8080/api/users/45/set-avatar" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file=@nyc.jpg;type=image/jpeg"
Request URL
http://localhost:8080/api/users/45/set-avatar

标签: javascriptvue.jsaxios

解决方案


推荐阅读