2017-08-01 31 views
1

我使用Dropzonejs,我想要做的事情本质上很简单。但是我找不到任何关于它的文章。Dropzonejs触发错误并取消上传

因此,在发送文件时,我们会在讨厌的.exe和.php文件上触发错误。 dropzonejs接口显示X和错误消息。所以这是正确的。问题在于,它仍然被抛入成功事件中,并被上传。

uploader.on("sending", function (file, xhr, data) { 

    var aDeny = [ // deny file some extensions by default 
     'application/x-msdownload', 
     'text/php' 
    ]; 

    if($.inArray(file.type, aDeny) !== -1) { 
     this.defaultOptions.error(file, 'File not allowed: ' + file.name); 
     return false; 
    } 
}); 

Evil.exe仍然出现在这个成功事件中并被上传。响应只有一个文件路径字符串,file.status是成功的。

uploader.on('success', function (file, response) { 

    getData({'dostuff'}); 

    return file.previewElement.classList.add("dz-success"); 
}); 

所以在我的“发送”事件中,如何防止文件出现在成功事件中?

更新:

谢谢!这是我需要的到底是什么:

var aDeny = [ // deny file some extensions by default 
    'application/x-msdownload', 
    'text/php' 
]; 

Dropzone.options.uploadWidget = { 
    // more config... 
    accept: function (file, done) { 
     if ($.inArray(file.type, aDeny) !== -1) { 
      done("This file is not accepted!"); 
     } 
     else { 
      done(); 
     } 
    } 
} 

回答

1

我首先总是检查的服务器端文件类型,以防止任何问题。

接受检查文件的MIME类型或针对此列表 扩展的默认实现:

然后与悬浮窗您可以使用过滤的文件类型。这是一个逗号分隔的mime 类型或文件扩展名列表。

如:图像/ *,应用程序/ PDF,.PSD

如果悬浮窗可点击此选项也将被用来作为接受隐藏的文件输入 参数为好。

样品:

var myDropzone = new Dropzone("div#myId", { 
    url: "/file/post", 
    acceptedFiles: 'application/x-msdownload,text/php' 
}); 

是获取文件和进行功能参数的函数。

如果done函数调用时没有参数,则文件为 “accepted”并将被处理。如果您传递错误消息, 文件将被拒绝,并显示错误消息。这个 功能将不会被调用,如果该文件太大或不匹配 的MIME类型。

样品:

Dropzone.options.myAwesomeDropzone = { 
    paramName: "file", // The name that will be used to transfer the file 
    maxFilesize: 2, // MB 
    accept: function(file, done) { 
    if (file.name == "justinbieber.jpg") { 
     done("This file is not accepted!"); 
    } 
    else { done(); } 
    } 
}; 
+0

所以,你找到了正确的解决方案:) – codtex