2016-07-14 73 views
0

我有以下代碼,使用dropzonejs上傳excel表單,但最大文件數爲1,只接受excel類型... 當多個文件上傳到正確的文件類型時,首先啓動錯誤,並由alert('please enter correct file format')生成消息,然後僅通過alert('You cannot upload more then 1 file at a time.')提醒真實錯誤消息。所以,我的問題是如何顯示真正的錯誤消息時初始化錯誤...在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> 

回答

2

不正確的文件類型的消息是因爲你與你的if語句,你會明顯地陷入的2個條件給出一個錯誤。

因此,你應該使用:

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

工作就像一個魅力....謝謝你的回答 –

+0

我想你不會遇到任何問題與IE11呢?我目前面臨的問題是type只是一個空字符串,但我在其他瀏覽器中沒有這樣的問題。在這方面努力尋找網上的任何東西。謝謝! –

+0

我剛剛在他們的Github上發現了一些東西,其實只是爲了將來的參考,任何人都需要它https://github.com/enyo/dropzone/issues/1143 –