Dropzonejs触发错误并取消上传
Dropzonejs trigger error and cancel upload
我正在使用 Dropzonejs,我想做的事情本质上很简单。但我找不到任何关于它的文章。
因此,在发送文件时,我们会在讨厌的 .exe 和 .php 文件上触发错误。 dropzonejs 界面显示 X 和错误消息。所以这是正确的。问题是它仍然被扔进 on success 事件,并被上传。
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");
});
所以在我的 'sending' 事件中,如何防止文件出现在成功事件中?
更新:
谢谢!这就是我最终需要的:
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();
}
}
}
我会首先检查服务器端的文件类型以防止出现任何问题。
然后要使用 Dropzone 过滤文件类型,您可以使用:
The default implementation of accept checks the file's mime type or
extension against this list. This is a comma separated list of mime
types or file extensions.
Eg.: image/*,application/pdf,.psd
If the Dropzone is clickable this option will also be used as accept
parameter on the hidden file input as well.
样本:
var myDropzone = new Dropzone("div#myId", {
url: "/file/post",
acceptedFiles: 'application/x-msdownload,text/php'
});
- accept 函数
A function that gets a file and a done function as parameters.
If the done function is invoked without arguments, the file is
"accepted" and will be processed. If you pass an error message, the
file is rejected, and the error message will be displayed. This
function will not be called if the file is too big or doesn't match
the mime types.
样本:
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(); }
}
};
我正在使用 Dropzonejs,我想做的事情本质上很简单。但我找不到任何关于它的文章。
因此,在发送文件时,我们会在讨厌的 .exe 和 .php 文件上触发错误。 dropzonejs 界面显示 X 和错误消息。所以这是正确的。问题是它仍然被扔进 on success 事件,并被上传。
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");
});
所以在我的 'sending' 事件中,如何防止文件出现在成功事件中?
更新:
谢谢!这就是我最终需要的:
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();
}
}
}
我会首先检查服务器端的文件类型以防止出现任何问题。
然后要使用 Dropzone 过滤文件类型,您可以使用:
The default implementation of accept checks the file's mime type or extension against this list. This is a comma separated list of mime types or file extensions.
Eg.: image/*,application/pdf,.psd
If the Dropzone is clickable this option will also be used as accept parameter on the hidden file input as well.
样本:
var myDropzone = new Dropzone("div#myId", {
url: "/file/post",
acceptedFiles: 'application/x-msdownload,text/php'
});
- accept 函数
A function that gets a file and a done function as parameters.
If the done function is invoked without arguments, the file is "accepted" and will be processed. If you pass an error message, the file is rejected, and the error message will be displayed. This function will not be called if the file is too big or doesn't match the mime types.
样本:
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(); }
}
};