I am faced with a challenge in storing image files and video files as Blob data.
One thing I like is that the uploaded video and image files display instantly, which is pretty cool.
I'm not entirely sure if the code below is correct - how can I navigate around this issue? My tech stack includes VueJs and Laravel.
Additionally, when I call the reset method on the addMessage function, it prevents the subsequent addition of another file. Could this be the right place to call it?
Here is the script in my Vue component:
<script>
import { Datetime } from 'vue-datetime';
export default {
components: { Datetime },
data() {
return {
text: '',
imageUrl: '',
imageBlob: '',
videoUrl: '',
videoBlob: '',
startTime: '',
endTime: '',
}
},
methods: {
reset(){
this.text = '';
this.imageUrl = '';
this.imageBlob = '';
this.videoUrl = '';
this.videoBlob = '';
this.startTime = '';
this.endTime = '';
},
refreshImage() {
let comp = this;
this.readObjectUrl($('#input-image').get(0), function (url, blob) {
comp.imageUrl = url;
comp.imageBlob = blob;
});
},
refreshVideo() {
let comp = this;
this.readObjectUrl($('#input-video').get(0), function (url, blob) {
comp.videoUrl = url;
comp.videoBlob = blob;
comp.playVideo(url);
});
},
playVideo(url) {
let video = $('#video-preview').get(0);
video.preload = 'metadata';
// Load video in Safari / IE11
if (url) {
video.muted = false;
video.playsInline = true;
video.play();
}
},
addMessage() {
this.$emit('added-message', this);
this.reset();
},
readDataUrl(input, callback) {
if (input.files && input.files[0]) {
let fileReader = new FileReader();
fileReader.onload = function () {
callback(fileReader.result);
};
fileReader.readAsDataURL(input.files[0]);
}
else {
callback(null);
}
},
readObjectUrl(input, callback) {
if (input.files && input.files[0]) {
let fileReader = new FileReader();
fileReader.onload = function () {
let blob = new Blob([fileReader.result], {type: input.files[0].type});
let url = URL.createObjectURL(blob);
callback(url, blob);
};
fileReader.readAsArrayBuffer(input.files[0]);
}
else {
callback(null);
}
},
}
}
Everything seems to work fine except for the file uploading functionality.
Thank you for your help!