gpt4 book ai didi

javascript - Dropzonejs触发错误并取消上传

转载 作者:行者123 更新时间:2023-11-29 21:03:17 27 4
gpt4 key购买 nike

我正在使用 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");
});

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

更新:

谢谢!这就是我最终需要的:

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'
});

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(); }
}
};

关于javascript - Dropzonejs触发错误并取消上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45438626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com