【问题标题】:File path null, Vue Js, Laravel, S3文件路径空,Vue Js,Laravel,S3
【发布时间】:2021-10-05 08:25:42
【问题描述】:

我尝试使用 Vuejs 和 Laravel 创建一个简单的文件上传。但我的“b2b_file”变量似乎为空。在 Vue 组件中我有:

<b-form-file
    v-model="b2b_file"
    name="b2b_file"
    multiple>
</b-form-file>

methods: {
    this.b2b_file.map(element => {
        this.productData.b2b_file.push(element)
    })
}

console.log(this.productData.b2b_file) 返回以下内容: https://prnt.sc/1hox1c5

由于当我尝试$request-&gt;file('b2b_file')-&gt;store() 时控制器中的路径为空,我收到消息:“在 null 上调用成员函数 store()”,异常:“错误”

我做错了什么?

【问题讨论】:

  • 我认为你必须使用v-bind,像这样:name="b2b_file",你缺少':'

标签: laravel vue.js amazon-s3 file-upload filepath


【解决方案1】:

经过一番激烈的搜索后,我发现我遗漏了一些逻辑,这就是我在控制器中得到 null 的原因。事实证明,在调用控制器之前,在 Vue 组件中我错过了这个:

  const formData = new FormData()
const file = this.productData.b2b_file
    formData.append('b2b_file', file)

axios.post('/api/auth/upload_file', formData)
      .then(response => {
        this.file_path = response.data
      })

在另一个 Axios 调用中,我发送了 this.file_path 的值 这样我就得到了文件的路径,$request-&gt;file('b2b_file')-&gt;store() 可以正常工作

【讨论】:

    猜你喜欢
    • 2021-07-11
    • 2017-12-16
    • 2019-10-27
    • 1970-01-01
    • 2016-03-19
    • 2020-02-18
    • 2015-03-27
    • 2019-09-07
    • 2021-11-22
    相关资源
    最近更新 更多