admin管理员组文章数量:1278690
HTML
<form enctype="multipart/form-data" method="post" id='photo_details'>
<input type="text" name="f_name">
<input type="file" accept="image/*" name="profile_pic">
<button class="btn">Save</button>
</form>
JAVASCRIPT
when I click submit of the form then given below javascript runs -
let form_details = document.getElementById("photo_details");
let form_data = new FormData(form_details);
// getting form values as key-value pair
for ([key, value] of form_data.entries()) {
console.log(key + ': ' + value);
}
Suppose I gave f_name as 'Carl' and profile_pic as 'my_passport.png' then
OUTPUT
f_name: Carl
profile_pic: [object File]
Problem in output -
I got the value of f_name
but
I am not getting the value of key profile_pic
instead I am getting [object File]
.
How to get file name using FormData object.
Help here !
HTML
<form enctype="multipart/form-data" method="post" id='photo_details'>
<input type="text" name="f_name">
<input type="file" accept="image/*" name="profile_pic">
<button class="btn">Save</button>
</form>
JAVASCRIPT
when I click submit of the form then given below javascript runs -
let form_details = document.getElementById("photo_details");
let form_data = new FormData(form_details);
// getting form values as key-value pair
for ([key, value] of form_data.entries()) {
console.log(key + ': ' + value);
}
Suppose I gave f_name as 'Carl' and profile_pic as 'my_passport.png' then
OUTPUT
f_name: Carl
profile_pic: [object File]
Problem in output -
I got the value of f_name
but
I am not getting the value of key profile_pic
instead I am getting [object File]
.
How to get file name using FormData object.
Help here !
-
2
Is that only for
console
? If so, don't concatenate to string but use multiple params:console.log(key , value);
. This way you'll have the actual File object getting logged, and you'll be able to navigate it to get itsname
property. – Kaiido Commented Jan 9, 2020 at 6:55 - let me try..... – Abhishek kamal Commented Jan 9, 2020 at 6:57
- It worked.. Some times I really forget for silly mistakes #myMistake – Abhishek kamal Commented Jan 9, 2020 at 7:02
- Thanks.. So this question is over here ! – Abhishek kamal Commented Jan 9, 2020 at 7:03
4 Answers
Reset to default 3The form contains two inputs, one of type text
and the other one of type file
which means that the FormData
instance will have two entries, one that has a string
value and the other one a File
value.
To get the file's name, you can check if the current entries value if an instance of File
and access its name
property. Here is an example:
for ([key, value] of form_data.entries()) {
let val;
if (value instanceof File) {
val = value.name;
} else {
val = value;
}
console.log(key + ': ' + val);
}
You can also get the file's name directly from the input, here is an example:
const fileInput = document.querySelector('input[type=file]');
const path = fileInput.value;
const fileName = path.split(/(\\|\/)/g).pop();
console.log('File name:', fileName);
This second example will only work if there is only one input of type file in the entire document.
<form enctype="multipart/form-data" onsubmit="return getData()" method="post" id='photo_details'>
<input type="text" name="f_name">
<input type="file" accept="image/*" name="profile_pic" id="file">
<input type="submit">
</form>
<script type="text/javascript">
function getData() {
let file_path = document.getElementById('file').value;
let file_name = file_path.split(/(\\|\/)/g).pop();
console.log(file_name);
return false;
}
</script>
try
form_data.get('profile_pic').name
photo_details.onchange = () => {
let form_data = new FormData(photo_details);
let fileName = form_data.get('profile_pic').name;
console.log({fileName});
}
<form enctype="multipart/form-data" method="post" id='photo_details'>
<input type="text" name="f_name">
<input type="file" accept="image/*" name="profile_pic">
<button class="btn">Save</button>
</form>
<form enctype="multipart/form-data" method="post" id='photo_details'>
<input type="text" name="f_name">
<input type="file" accept="image/*" name="profile_pic">
<button class="btn" type="button" onclick="myFunction()">Save</button>
</form>
<script type="text/javascript">
function myFunction(){
let form_details = document.getElementById("photo_details");
let form_data = new FormData(form_details);
for ([key, value] of form_data.entries()) {
console.log(value);
}
}
</script>
Tested and works.
本文标签: javascriptHow to get filename using FormData objectStack Overflow
版权声明:本文标题:javascript - How to get filename using FormData object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262128a2367822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论