【问题标题】:How can I post file and data together via Axios?如何通过 Axios 将文件和数据一起发布?
【发布时间】:2020-08-14 14:39:19
【问题描述】:

我正在尝试发布数据(在 Vue 应用程序中)。表单中还有一个图像文件输入。所有教程和答案都只是告诉单独发送文件或与其他文件一起发送。[1][2]

我想要做的是将文件附加到我使用 v-model 绑定创建的现有对象中。

// Vue

<template>
    <input name="title" type="text" v-model="newPost.title" />
    <textarea name="content" v-model="newPost.content"></textarea>
    <input name="image" type="file" />
</template>
<script>
    ...
    newPost: {
        title: null,
        image: null,
        content: null
    }
    ...
    addNewPost() {
        axios.post('/api/posts', this.newPost)
    }
</script>

我应该怎么做?

【问题讨论】:

标签: vue.js axios


【解决方案1】:

您可以在客户端使用 Base64 编码并将编码字符串添加到您的发布请求并从服务器端解码: 此处的图像将是编码字符串,您可以在编写时发送 axios 请求。

【讨论】:

  • 我会尽快试一试的!
猜你喜欢
  • 1970-01-01
  • 2022-01-19
  • 2021-11-13
  • 2021-03-30
  • 2022-01-27
  • 2015-02-13
  • 2012-08-18
  • 2016-12-28
  • 2017-08-11
相关资源
最近更新 更多