1

I have created a file select functionality that allows the user to select a .csv files. Code is below:

<input type="file" accept=".csv" ref="file" v-on:input="handleFileUpload()" id="upload-photo" /> 

This enables the user to browse for .csv files and my problem is, if the user select the all files, all files are displayed and can select other file types.

import box

How can I disable the all files option or check the file extension using vue.js. Below is the functionality if the user selects the file.

onConfirm() {
        this.txtBrowse = this.file.name; //gets the file name with the extension
        //other functionalities
}

UPDATE

As of the moment, I temporarily used the following trapping:

if(this.file.name.split(".").pop() != 'csv'){//check if file extension is csv
   Vue.alert.show( "Please select CSV file type", "error");
   return;
}

But, if you have a better approach, please help me out.

1

2 Answers 2

2

I was in a similar situation and have solved it the following way. For inputting the file:

<input id="fileUpload" type="file" ref="inputFile" @input="setUploadFile()" hidden />

Inside the setUploadFile function:

setUploadFile(){
 this.file = this.$refs.inputFile.files[0];
 if (this.file.type == "text/csv"){
    // Api call
 }
 else {
   this.showAlert("error", "The file-type doesn't belong to text/csv")
 }
}
Sign up to request clarification or add additional context in comments.

Comments

-1
if(this.file.name.split(".").pop() != 'csv'){//check if file extension is csv
   Vue.alert.show( "Please select CSV file type", "error");
   return false;
}`enter code here`

1 Comment

Please add some explanation to your answer such that others can learn from it. To me, it looks like you've simply copied something from the question that does not disallow any user to select files of other types

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.