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:
Result



enter image description here





this.file=e.target.files[0] & @change="fileChange($event)" should work
– Helping hand
Aug 24 at 17:17


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)