【发布时间】:2020-10-13 13:25:09
【问题描述】:
我刚刚用“头像”字段更新了我的用户模型,这样我就可以上传照片了。我使用了一个已经配置好的 PUT 方法,并在其中添加了头像。在邮递员中,文件上传(表单数据)工作得很好,但是当尝试使用来自 vue.js 的 axios 上传它时它不起作用。我尝试了很多方法,最后一种,我尝试将请求作为多表单数据发送。
async saveChanges() {
const fd = new FormData();
fd.append("id", this.$auth.user().id);
fd.append("username", this.$auth.user().username);
fd.append("email", this.user.email);
fd.append("firstName", this.$auth.user().firstName);
fd.append("lastName", this.$auth.user().lastName);
fd.append("isAdmin", this.$auth.user().isAdmin);
fd.append("password", this.user.password);
fd.append("confirmpass", this.user.confirmpass);
fd.append("avatar", this.selectedFile, this.selectedFile.name);
fd.append("_method", "put");
try {
await this.axios.put(`/users/${this.$auth.user().id}`, {
fd
}).then((res) => {
console.log(res);
});
} catch (err) {
console.error(err);
}
}
在我选择文件后,它是可用的,但我无法通过我的方法发送它。我应该创建另一个请求来更新头像还是可以解决这个问题?
【问题讨论】:
标签: javascript node.js vue.js axios