1

I'm trying to use ExcelJS in Vue and I need FileReader to read and parse the files but I'm getting errors. How do I use FileReader with VueJS?

Input Form

    <input type="file" 
        id="importProductionSchedule" 
        name="importProductionSchedule" 
        @change="checkFile($event)" 
        ref="importProductionSchedule"
    >

checkFile method

    checkFile() {
        let reader = new FileReader()
        let self = this

        reader.onload = (e) => {
            let bstr = e.target.result
            let wb = XLSX.read(bstr, {type:'binary'})
            let wsname = wb.SheetNames[0]
            let ws = wb.Sheets[wsname]
            let data = XLSX.utils.sheet_to_json(ws, {header:1})
            self.form.filedata = data
            self.cols = make_cols(ws['!ref'])
        }
        reader.onerror = (stuff) => {
            console.log("error", stuff)
            console.log (stuff.getMessage())
        }
        // reader.readAsArrayBuffer(event)
        reader.readAsBinaryString(event.target.files[0])
    },

First of all, logging event.target.files[0] in the console would return the file, but I'm testing both event and event.target.files[0] to make sure.

These are my errors:

    event = Uncaught Error: cannot read as File: {"isTrusted":true}
    event.target.files[0] = Uncaught Error: cannot read as File: {}
9
  • I think you just need to receive the event here checkFile(event) {... Commented Jan 29, 2018 at 4:46
  • @RichardMatsen regardless if I add the event or argument or not, the event variable is passed. I tested both ways and both log results of the same event data Commented Jan 29, 2018 at 5:13
  • I'm not sure how the event gets in if you don't have a parameter. I guess if you put a console.log before the reader.readAsBinaryString( line, you get correct info about your file? Commented Jan 29, 2018 at 5:16
  • I tried this code: reader.readAsArrayBuffer(event.target.files), then the error returns: Uncaught Error: cannot read as File: {"0":{}} It seems like object inside the target.files is empty event though it has a file attached onto it already Commented Jan 29, 2018 at 5:55
  • Sorry, I got lost when you told me the event was getting picked up even though it's not passed in. Normally in Vue events are handled with v-on: syntax, not @ syntax. Commented Jan 29, 2018 at 6:04

3 Answers 3

2

you can use below method

createImage(file) {
    let reader = new FileReader()
    reader.onload = (event) => {
        this.product.image = event.target.result
    }
    reader.readAsDataURL(file)
}
Sign up to request clarification or add additional context in comments.

1 Comment

This seem to only work for me on images. what about not image files? I've tried readAsBinaryString, readAsArrayBuffer and readAsText none of them seem to work.
0
    methods:{
        uploadImage(event) {
        const image = event.target.files[0];
        const reader = new FileReader();
        reader.readAsDataURL(image);
        reader.onload = (event) => {
        this.previewImage = event.target.result;
       };
     }, 
 }

Comments

0
<input class="patientPorting"
type="file"
@change="onFilePicked( $event )"/>

Create below method to get the file.

const onFilePicked = (event) =>{

        const fData = event.target.files[0];
        const reader = new FileReader();
        reader.readAsDataURL(fData);
        reader.onload = (event) => {
            fileData = event.target.result;
        };
        console.log(fileData);
        jsonObj = [];
        jsonObj.push({
            "fileData" : fileData,
            "fileName":fData.name,
            "fileSize":fData.size,
            "fileType":fData.type,
        }
       );
    }

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.