6

I am building a component which has file input fields and being rendered through function in VueJs:

export default {
    name: 'nits-file-input',
    props: {
        label: String,
    },
    render (createElement) {
        return createElement('div', { class: 'form-group m-form__group'}, [
            createElement('label', this.label),
            createElement('div'),
            createElement('div', { class: 'custom-file'},[
                createElement('input', {
                    class: 'custom-file-input',
                    attrs: { type: 'file' },
                    domProps: {
                        value: self.value
                    },
                    on: {
                        input: (event) => {
                            var reader = new FileReader()
                            reader.readAsDataURL(event.target.value)
                            reader.onload = function () {
                                console.log(reader.result);
                            };
                            this.$emit('input', event.target.value)
                        }
                    }
                }),
                createElement('label', { class: 'custom-file-label'}, 'Choose File')
            ])
        ])
    }
}

While having the values in v-model I get the path of file but I need to have a base64 element. currently in my console I'm getting following error:

Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'

file upload error

Help me out in execution. Thanks

4
  • i think that the size is big and it's not supported Commented Dec 27, 2018 at 11:17
  • @BoussadjraBrahim File size is 96 KB. I'm using smallest file to do the testing. Commented Dec 27, 2018 at 11:25
  • try a file with size under 4kb Commented Dec 27, 2018 at 11:35
  • @BoussadjraBrahim I tried 22 bytes. It still showing same error. Commented Dec 27, 2018 at 11:39

2 Answers 2

11

You should set reader.readAsDataURL(event.target.files[0])

instead of

reader.readAsDataURL(event.target.value) :

on: {
    input: (event) => {
        var reader = new FileReader()
        reader.readAsDataURL(event.target.files[0])
        reader.onload = () => {
            console.log(reader.result);
        };
        this.$emit('input', event.target.files[0])
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I'm unbale to assign base64 value inside my $emit function. I am using let baseFile = '' reader.onload = function () { baseFile = reader.result }; this.$emit('input', baseFile) can you help with that?
i hope that i could, please post a new question where you explain your use case and the issue that you're getting
3

Change to reader.readAsDataURL(event.target.files[0])

Following post could be helpful https://alligator.io/vuejs/file-reader-component/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.