繁体   English   中英

FormData没有追加文件对象

[英]FormData is not appending file object

我有一个在<input type="file/>的onChange事件之后执行的函数dataz console.log(dataz)axios.post之后, dataz变量为空

 const input = document.getElementById('input'); input.addEventListener('change', handlefileupload); function handlefileupload(e) { var dataz = new FormData(); dataz.append('file', e.target.files[0]); console.log(dataz); /* Note to OP: you dont need to actually include code like this, it has nothing to do with your question/problem axios.post('http://localhost:3000/api/v1/upload_document', { files: dataz }, { headers: { Authorization: "Bearer " + sessionStorage.getItem('loginToken'), 'Content-Type': 'multipart/form-data' } }) .then((response) => { console.log('success') } ) .catch(function(error) { console.log(error.response); }); */ } 
 <input type="file" id="input" /> 

这就是我在console.log(dataz)

FormData {}
__proto__: FormData
append: ƒ append()
delete: ƒ delete()
entries: ƒ entries()
forEach: ƒ forEach()
get: ƒ ()
getAll: ƒ getAll()
has: ƒ has()
keys: ƒ keys()
set: ƒ ()
values: ƒ values()
constructor: ƒ FormData()
Symbol(Symbol.iterator): ƒ entries()
Symbol(Symbol.toStringTag): "FormData"
__proto__: Object

用于对输入字段进行操作的事件处理程序称为“输入”。 所以我怀疑你想要什么:

input.addEventListener('input', handlefileupload);

您不能使用console.log(dataz)显示FormData值。 代替此使用以下内容:

    for (let val of dataz.values()) {
        console.log(val);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM