FormData 为空,但文件正常

FormData is null but file is ok

(前面vuejs3 + axios | 后面nodejs + prisma sql)

我有一个函数可以创建 post。当我将它与 postman 一起使用时,它起作用了。

但是当我在前端使用它时 returns me a formdata = null

在 this.selectedFile,我有文件

你能帮帮我吗?

data () {
    return {
        title: '',
        content: '',
        userId: '',
        selectdFile: null,
    }
},
methods: {
    onFileSelected (event) {
        this.selectedFile = event.target.files[0];
        console.log(this.selectedFile);
    },
    async createPost() {
        const formData = new FormData();
        formData.append( 'image', this.selectedFile, this.selectedFile.name );

        console.log('ok')
        console.log(formData);
        const id = localStorage.getItem('userId');
        const response = await axios.post('http://localhost:3000/api/post', {
            title: this.title,
            content: this.content,
            userId: parseInt(id),
            attachment: formData,

        }, {
            headers: {
                Authorization: 'Bearer ' + localStorage.getItem('token')
            },
        });
        console.log(response);
    },

结果声音:

试试这个:

const response = await axios.postForm(
  "http://localhost:3000/api/post",
  {
    title: this.title,
    content: this.content,
    userId: parseInt(id),
    attachment: this.selectedFile,
  },
  {
    headers: {
      Authorization: "Bearer " + localStorage.getItem("token"),
    },
  }
);