2016-07-14 11 views
0

Ich habe folgenden Code zum Hochladen Excel Blatt mit Dropzonejs mit bestimmten Bedingung wie maximale Anzahl der Datei ist 1 und nur Excel-Typ wird akzeptiert ... wenn mehrere Dateien mit korrekten Dateityp dann hochgeladen wird Der Fehler wird zuerst ausgelöst und die Nachricht wird von alert('please enter correct file format') generiert und dann wird nur die echte Fehlermeldung von alert('You cannot upload more then 1 file at a time.') gemeldet. Also, meine Frage ist, wie echte Fehlermeldung angezeigt wird, wenn Fehler initialisiert ...Prüfe Dateityp in Dropzone

<script> 
    var myDropzone = new Dropzone("div#myAwesomeDropzone", 
    { url: "<?php echo base_url(); ?>test/upload_excel_file",maxFiles: 1, 
    acceptedFiles: ".xls,.xlsx",dictDefaultMessage: 'Drag an excel sheet here 
    to upload, or click to select one', 
    init : function(){this.on("maxfilesexceeded",function(file) 
    { 
    alert('You cannot upload more then 1 file at a time.'); 
    this.removeFile(file); 
    }); 

    this.on("error",function(file) 
    { 
    var type = file.type; 
    //alert(type); 
    if(type != 'application/vnd.ms-excel') 
    { 
    alert('please enter correct file format'); 
    this.removeFile(file); 
    } 
    else if(type != 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') 
    { 
    alert('please enter correct file format'); 
    this.removeFile(file); 
    } 
    }); 
    } 
    }); 
</script> 

Antwort

2

Die falsche Dateityp Nachricht, weil Sie geschieht mit if-Anweisungen werden Sie natürlich fallen in eine der zwei Bedingungen, die die Störung zu geben .

Anstatt also sollten Sie verwenden:

switch(file.type) 
{ 
    case 'application/vnd.ms-excel': 
    case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 
    break; 
    default: 
    alert('please enter correct file format'); 
    break; 
} 
+0

arbeitete wie ein Charme .... vielen Dank für Ihre Antwort –

+0

Ich glaube nicht, daß Sie mit IE11 überhaupt irgendwelche Probleme erlebt? Ich bin derzeit mit einem Problem konfrontiert, bei dem Typ nur eine leere Zeichenfolge ist, aber ich habe keine Probleme in anderen Browsern. Kämpfe darum, auch online etwas zu finden. Vielen Dank! –

+0

Ich habe gerade etwas auf ihrer Github tatsächlich nur für zukünftige Referenz gefunden, sollte jemand es brauchen https://github.com/enyo/dropzone/issues/1143 –