leewinder / ng2-file-drop

An Angular module for simple desktop file drag and drop with automatic file validation and dynamic style adjustment.
MIT License
28 stars 26 forks source link

Windows unexpected mime type #30

Open bryns1 opened 7 years ago

bryns1 commented 7 years ago

The problem:

I have a single roster.csv file synced between my PC and Mac. When uploading the file from windows, it doesn't return it's mime type in the file object, instead you get type:"". (Unless you have excel installed then it returns an excel specific type)

Examples of file types returned: Linux/Mac returns type:"text/csv" Windows returns type:"" (empty) Windows with Excel returns type:"application/vnd.ms-excel" (Assuming the mime type would change with other programs as well)

This makes it nearly impossible to let my app reliably accept .csv files across platform.

So I was wondering for a solution, would it be possible to use something similar to this?

let type = currentFiles[i].type;
if(!type){
    let textType = currentFiles[i].name.substring(currentFiles[i].name.lastIndexOf("."), 
    currentFiles[i].name.length); // based off https://stackoverflow.com/a/1203361/4105381, includes "." in file name
    type =  textType.length != currentFiles[i].name.length ? textType : undefined; // Returning undefined for readability, maybe return ""
}
// let fileTypeIndex: number = this.supportedFileTypes.indexOf(type);

https://codepen.io/bryn/pen/mwPrmB/

Obviously we can't set "" but [".csv", "text/csv"] would work

Another cool solution would be to allow developers to write their own validation functions that they can parse to File Drop.

function myCustomValidator(file){
   let myValidList = [".csv"];
   let textType = file.name.substring(file.name.lastIndexOf("."), file.name.length);
   let isValid = myValidList.indexOf(textType) !== -1;
   if(isValid){
      return {valid: true};
   }else{
       return {valid: false, reason:"reason"};
   }
}

I'm still relatively new to the coding scene. Tell me if any of this sounds doable/reasonable