I have a generic avatar preview modal:
<avatar-update :img-file="avatarFile" :show="avatarModalShow" :img-url="url" @close="avatarModalShow = !avatarModalShow" :change-avatar="updateCrop" @destroyUrl="imgUrl = null"> </avatar-update>
When an avatar is submitted I use my root to send a bunch of properties to the AvatarUpdate component.
HTML
<div>
<label for="avatar" class="cursor-pointer thumbnail-link bg-white p-1 rounded-lg" href="">
<img class="thumbnail" src="{{asset('images/avatars/avatar-1.png')}}">
</label>
<input id="avatar" class="hidden" type="file" @change="onFileChange"/>
</div>
Root
onFileChange: function(e) {
const file = e.target.files[0];
this.url = URL.createObjectURL(file);
this.updateCrop = !this.updateCrop;
this.avatarModalShow = !this.avatarModalShow;
this.avatarFile = file;
},
When I console.log the file const in the onFileChange function I get the file object. However, when I try and output the {{imgFile}}
property in the AvatarUpdate component I get an empty object.
I'm wondering if this is safe and if the file data can be manipulated between the root and AvatarUpdate component? Also is there something preventing me from being able to send and output the file object as a property? Why is it giving me an empty object on the AvatarUpdate component?
I'm sorry for so many questions, but my reasoning for including them into one post is that I think there could be some security functionality preventing me from sending the file object through a component.
Edit
Here is my AvatarUpload component:
<modal v-show="show" heading="Avatar Preview" @close="close">
<div class="flex flex-col">
<h4 class="text-blue-light mb-5">The avatar will be automatically cropped from the center.</h4>
<div class="flex flex-col items-center">
<img class="w-2/3" :src="imgUrl">
</div>
<p>{{imgFile}}</p>
<button class="mt-4 h-10 self-end text-center bg-third-color hover:bg-secondary-color text-white font-bold py-2 px-4 rounded" v-on:click="submitAvatar()">Submit</button>
</div>
<script>
export default {
props: ['show','imgUrl','changeAvatar','imgFile'],
data() {
return {
image: null,
message: null
}
},
methods: {
close: function(){
this.$emit('close');
},
submitAvatar: function(){
console.log(file);
axios({
method: 'POST',
url: '/profile/avatar',
data: {},
}).then(function (response) {
this.message = "Your avatar has been submitted";
}.bind(this))
.catch(function (error) {
console.log(error);
});
}
}
}
</script>
I already am able to obtain the blob from this.url = URL.createObjectURL(file);
in the onFileChange function on the root instance. What I am trying to do is send the whole file object to the AvatarUpdate component using the :img-file="avatarFile"
prop.
This way I can send the data in a way that can be accessed on the request in a Laravel controller:
submitAvatar: function(){
//Change here!
var data = new FormData()
var file = this.imgFile;
data.append('avatar', file);
axios({
method: 'POST',
url: '/profile/avatar',
data: data,
}).then(function (response) {
this.message = "Your avatar has been submitted";
}.bind(this))
.catch(function (error) {
console.log(error);
});
}
Laravel UserController
UserController
public function avatar(Request $request)
{
return $request->hasFile('avatar');
}