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.

Eem Jee
  • 1,239
  • 5
  • 30
  • 64

2 Answers2

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")
 }
}
srijan439
  • 401
  • 5
  • 7
-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`
  • 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 – Nico Haase Apr 09 '19 at 06:50