2013-08-06 32 views
0

我使用Dropzone.js,併爲拒絕具有相同文件名的文件的文件實現了自定義約束。我的繼承人初始化代碼:Dropzone.js以編程方式調用accept()函數

var myDropzone = new Dropzone("div#myId", { 
    url: MpgCommon.app.route('upload_files_path'), 
    addRemoveLinks: true, 
    thumbnailWidth: "80", 
    thumbnailHeight: "80", 
    dictCancelUpload: "Cancel", 
    parallelUploads: 12, 
    uploadMultiple: false, 
    autoProcessQueue: false, 
    accept: function(file, done) { 
    var selected_filenames = $.map(myDropzone.files, function(selected_file) { 
     return (selected_file.name.replace(/\.[^.]+$/, '')); 
    }); 
    var filename = file.name.replace(/\.[^.]+$/, '') 
    if ($.inArray(filename, selected_filenames) != selected_filenames.length-1){ 
     var error_msg = "Error!"; 
     $('.edit_tooltip .gui_message.error').css({display: "block"}).html(error_msg); 
     done(error_msg); 
    } 
    else { 
     $('.edit_tooltip .gui_message.error').css({display: "none"}).html(''); 
     done(); 
    } 
    } 
}); 

現在看來,當添加的文件被接受函數被調用而不是當一個文件從懸浮窗中刪除。那麼是否可以通過編程來調用accept函數?

我嘗試這樣做,但它不到風度工作:

myDropzone.on("removedfile", function(file) { 
    var rejected_files = myDropzone.getRejectedFiles(); 
    console.log(rejected_files); 
    rejected_files.forEach(function() { 
    console.log(this); 
    myDropzone.accept(this); 
    }); 
}); 

我在控制檯得到的錯誤是:

TypeError: done is not a function 
if (args) { 

回答

0

這是一個不太理想的解決方案,但我發現,如果您以編程方式模擬現有文件的拖放功能,則可以實現類似的結果'重新處理現有的fil ES'。

這涉及到文件存儲到一個變量,從懸浮窗對象中刪除文件和添加文件(縣)回從你的JavaScript文件來執行:

// Single file example. For multiple use dropzone.addFiles(dropzone.files); 
var singleFile = dropzone.files[0]; 
if (singleFile) { 
    dropzone.removeAllFiles(false); 
    dropzone.addFile(singleFile); 
} 
相關問題