我使用最新版本(9.11.2)上传文件。我已经实现了一个按钮,供用户单击以使用add
选项开始上传。现在我还想添加文件验证以确保文件是图像。 I found a solution to to this。这对我来说非常合适,但前提是我删除了下面的add
选项。
$('#fileupload').fileupload({
forceIframeTransport: true,
url: nexus.admintoolapi + 'user/picture',
// The regular expression for allowed file types, matches
// against either file type or file name:
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
add: function (e, data) {
$uploadButton.show();
$uploadButton.on('click', function () {
data.submit();
});
},
success: function (e, data) {
},
fail: function (e, data) {
},
done: function (e, data) {
}
}).on('fileuploadprocessalways', function (e, data) {
var currentFile = data.files[data.index];
if (data.files.error && currentFile.error) {
// there was an error, do something about it
console.log(currentFile.error);
}
});
因此上述代码根本不会运行.on('fileuploadprocessalways'
。但这会:
$('#fileupload').fileupload({
forceIframeTransport: true,
url: nexus.admintoolapi + 'user/picture',
// The regular expression for allowed file types, matches
// against either file type or file name:
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
success: function (e, data) {
},
fail: function (e, data) {
},
done: function (e, data) {
}
}).on('fileuploadprocessalways', function (e, data) {
var currentFile = data.files[data.index];
if (data.files.error && currentFile.error) {
// there was an error, do something about it
console.log(currentFile.error);
}
});
如果我有这个,那么将捕获错误的文件结尾并停止文件上传,但用户无法主动开始上传。
我还尝试过fileuploadprocessalways
的代码,但currentFile
和data.files.error
未定义。
add: function (e, data) {
$uploadButton.show();
$uploadButton.on('click', function () {
var currentFile = data.files[data.index];
if (data.files.error && currentFile.error) {
// Here no error occurs ever, both conditions are undefined
console.log(currentFile.error);
} else {
data.submit();
});
},