2013-08-30 55 views
0

我從plupload得到了一個上傳腳本,我想編輯的東西,但我不知道如何。 而不是隻允許某些類型的文件我想限制只有一些擴展名,如PHP,EXE,CMD,蝙蝠。javascript過濾器:擴展到過濾器限制

var uploader = new plupload.Uploader({ 
    runtimes : 'html5,flash,silverlight', // Set runtimes, here it will use HTML5, if not supported will use flash, etc. 
    browse_button : 'pickfiles', // The id on the select files button 
    multi_selection: false, // Allow to select one file each time 
    container : 'uploader', // The id of the upload form container 
    max_file_size : '800kb', // Maximum file size allowed 
    url : 'upload.php', // The url to the upload.php file 
    flash_swf_url : 'js/plupload.flash.swf', // The url to thye flash file 
    silverlight_xap_url : 'js/plupload.silverlight.xap', // The url to the silverlight file 
    filters : [ {extensions : "zip,rar,jpg,gif,png,jpeg,"} ] // Filter the files that will be showed on the select files window 
}); 

我想將過濾器更改爲限制。任何人都可以幫助我嗎?

@Luan,我找到了解決辦法

if(invalidExtensions.indexOf(extension) >= 0){ 


     alert("the extension " + extension + " is invalid!"); 
     up.removeFile(files[i]); 
     location.reload(); 

    } 

感謝

+0

它看起來不像那個功能可用。 – Barmar

+0

你可以在這裏看到所有的選項:http://www.plupload.com/documentation.php – Barmar

回答

0

如果擴展名是無效的,您可以阿泰斯特和刪除文件...

var uploader = new plupload.Uploader({ 
    runtimes : 'html5,flash,silverlight', // Set runtimes, here it will use HTML5, if not supported will use flash, etc. 
    browse_button : 'pickfiles', // The id on the select files button 
    multi_selection: false, // Allow to select one file each time 
    container : 'uploader', // The id of the upload form container 
    max_file_size : '800kb', // Maximum file size allowed 
    url : 'upload.php', // The url to the upload.php file 
    flash_swf_url : 'js/plupload.flash.swf', // The url to thye flash file 
    silverlight_xap_url : 'js/plupload.silverlight.xap', // The url to the silverlight file 
    filters : [ {extensions : "zip,rar,jpg,gif,png,jpeg,"} ] // Filter the files that will be showed on the select files window 
}); 

var invalidExtensions = [".php",".exe",".cmd",".bat" ]; 
var extension; 

uploader.bind('FilesAdded', function(up, files) { 

    for(var i = 0; i < files.length; i++){ 

     extension = files[i].name.substr(-4).toLowerCase(); 

     if(invalidExtensions.indexOf(extension) >= 0){ 

      alert("the extension " + extension + " is invalid!"); 
      up.removeFile(files[i]); 

     } 

    } 

}); 

我認爲會工作。

+0

欒這工作,但它只是警告我,文件分機無效 –

+0

不是正在刪除?改變'up.removeFile(files [i]);'爲'setTimeout(function(){up.removeFile(files [i]);},100);'和teste ... –