【问题标题】:How to append file to forData object vue.js如何将文件附加到 forData 对象 vue.js
【发布时间】:2018-09-24 05:12:00
【问题描述】:

我正在尝试将文件从输入附加到 formData 对象,但是当我控制台记录该对象时,它是空的。我尝试了多种解决方案,但似乎无法找出问题所在。

文件输入

<input class="btn btn-sm btn-rounded btn-noborder btn-primary" type="file" @change="uploadReturnLabel($event)" :disabled="!editState"/>

方法

uploadReturnLabel(e) {
                if (!e.target.files.length) {
                    return;
                }
                var data = new FormData();
                data.append('return_label', e.target.files[0]);
                console.log(data);
                /*
                post('rma-return-label/'+this.rma.rma_ticket_id, data)
                    .then((res) => {
                        console.log(res)
                    })
                 */
            },

【问题讨论】:

  • 为什么不用v-model绑定输入值?
  • 您不能在文件输入上设置值。

标签: javascript vue.js vuejs2


【解决方案1】:

SO Post找到答案

要记录FormData,你可以使用entry()

for (var key of data.entries()) {
    console.log(key[0] + ', ' + key[1]);
}

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 2019-09-21
    • 1970-01-01
    • 2020-04-12
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2012-06-03
    相关资源
    最近更新 更多