How to store FileList object inside Vue data
How to store FileList object inside Vue data
Hello im trying to store a FileList object inside vue data object but it's being stored as string "File"
Im adding a ref to html like this
<input type="file" @change="fileChange($event.target)">
And in the javascript
new Vue(
el: '#someEl',
data:
file: null
,
methods:
fileChange (file)
this.file = file.files[0]
)
As a result i get this:
this.file=e.target.files[0]
@change="fileChange($event)"
Don't know vue, nor this console output, but are you sure this is a string? That would be a really strange string output (
[object File]
would be more expected here). Also, notpicking, but it is a File objevt you are trying to store here. The FileList is the object above.– Kaiido
Aug 25 at 10:03
[object File]
@Helpinghand that didn't work either :( it is still stored as [object File]
– Madtin
Aug 27 at 8:59
@Kaiido yes It's stored as [object File] when I double click it. the console Its because It's the vue developer tools.
– Madtin
Aug 27 at 8:59
1 Answer
1
You need to just pass argument $event
to fileChange()
instead of $event.target
.
Now you can access file object using evt.target.files[0];
inside fileChange()
function.
$event
fileChange()
$event.target
evt.target.files[0];
fileChange()
var vm = new Vue(
el: '#app',
data:
file:
,
methods:
fileChange (evt)
this.file = evt.target.files[0];
console.log('file Object:==>',this.file);
);
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js" type="text/javascript"></script>
<div id="app">
<p>
<input type="file" @change="fileChange($event)">
name: <b>file.name</b>
</p>
<p>
size: <b>file.size </b>
& type: <b>file.type</b>
</p>
</div>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
this.file=e.target.files[0]
&@change="fileChange($event)"
should work– Helping hand
Aug 24 at 17:17