假设我需要限制用户上传低于3MP(近2048像素宽)的图像。我怎么能在Dropzone.js中这样做?尝试用“接受”来做,但它不起作用:
$(function() {
var mediaDropzone;
mediaDropzone = new Dropzone("#media-dropzone", {
paramName: "file",
autoProcessQueue: false,
parallelUploads: 500,
maxFilesize: <%= ENV["max_image_size"] %>,
acceptedFiles: '<%= ENV["accepted_files"] %>',
accept: function(file, done) {
if (file.width < 2048) {
done("Naha, you don't.");
}
else { done(); }
}
});
答案 0 :(得分:4)
我有类似的需求并使用此解决方案:
var maxImageWidth = 1000, maxImageHeight = 1000;
Dropzone.options.dropzonePhotographs = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
dictDefaultMessage: "Drag files or click here",
acceptedFiles: ".jpeg,.jpg,.png,.gif",
init: function () {
this.on("success", function(file, responseText) {
file.previewTemplate.setAttribute('id',responseText[0].id);
});
this.on("thumbnail", function(file) {
if (file.width > maxImageWidth || file.height > maxImageHeight) {
file.rejectDimensions()
}
else {
file.acceptDimensions();
}
});
},
accept: function(file, done) {
file.acceptDimensions = done;
file.rejectDimensions = function() { done("Image width or height too big."); };
}
};
您可以在此处找到原始解决方案:https://github.com/enyo/dropzone/issues/708
答案 1 :(得分:0)
请求accept:
申请,您可以使用下面的thumbnail
您可以检查图像的宽度和高度,如
this.on("thumbnail", function (file) {
if (file.height < 350 && file.width < 2048) {
alert("Image should be 350 x 2048 ");
}
});