2014-11-24 41 views
2

比方说,我需要限制用户上传低于3MP(近2048px宽)的图像。我怎样才能在Dropzone.js中做到这一点?试图与“接受”做到这一点,但它不工作:在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(); } 
     } 
}); 
+1

仅仅通过File API接口(https://developer.mozilla.org/en-US/docs/Web/API/File)可能无法实现。你可以尝试一些画布库? – 2014-11-25 03:09:47

回答

1

我也有类似的需求,并正在使用此解决方案:

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

0

insted的在accept:申请可以使用thumbnail像下面

您可以检查宽度和高度图像像

this.on("thumbnail", function (file) { 
    if (file.height < 350 && file.width < 2048) { 
     alert("Image should be 350 x 2048 "); 
     } 
    }); 
相关问题