【问题标题】:How to get image dimension in Dropzone JS with setting createImageThumbnails to false?如何通过将 createImageThumbnails 设置为 false 在 Dropzone JS 中获取图像尺寸?
【发布时间】:2018-10-09 09:18:15
【问题描述】:

使用 Dropzone.js,我尝试使用 createImageThumbnails = false 获取图像高度和宽度。更准确地说,我不需要在删除图像时创建缩略图,因为当我一次删除许多图像时,这个过程会变得很慢。我只需要扫描所有被丢弃的图像的高度和宽度并将它们保存到数据库中。但问题是,当我关闭缩略图创建时,dropzone 不提供图像高度和宽度。根据文档,触发缩略图事件后会提供图像高度和宽度。所以,与我需要的完全相反。因此,作为一种解决方案,我希望能够在图像被放入 dropzone 后立即获得图像高度和宽度信息,并且创建缩略图应该没有延迟。请告知是否有出路。

HTML

<div id="dropzone">
    <form class="dropzone" id = "upload-widget" action = "/demo">
    </form>
</div>

JS

jQuery(document).ready(function($)
{
  var images = Array();

  var item = [];

  Dropzone.options.uploadWidget = {
    autoProcessQueue: false,
    acceptedFiles: 'image/*',
    previewTemplate: '<div class="dz-filename"><span data-dz-name></span></div>',
    createImageThumbnails: false,
    init: function() {
      this.on("addedfile", function(file)
      {
        height = file.height;
        width = file.width;
        item = {Name : file.name, Size:file.size, Height:file.height, Width:file.width};
        images.push(item);
      });
  }
  };

});

【问题讨论】:

    标签: javascript jquery dropzone.js


    【解决方案1】:

    您可以尝试使用accept 函数、FileReader()Image() 构造函数。

    Dropzone.options.uploadWidget = {
      autoProcessQueue: false,
      acceptedFiles: 'image/*',
      previewTemplate: '<div class="dz-filename"><span data-dz-name></span></div>',
      createImageThumbnails: false,
      accept: function(file, done) {
    
        // FileReader() asynchronously reads the contents of files (or raw data buffers) stored on the user's computer.
        var reader = new FileReader();
        reader.onload = (function(entry) {
          // The Image() constructor creates a new HTMLImageElement instance.
          var image = new Image(); 
          image.src = entry.target.result;
          image.onload = function() {
    
            console.log(this.width);
            console.log(this.height);
    
          };
        });
    
        reader.readAsDataURL(file);
        done();
      }
    }
    

    【讨论】:

    • 我建议使用URL.createObjectURL 而不是FileReader
    • 我使用了URL.createObjectURL,效果很好。无论哪种方式,这个答案都应该被标记为正确。
    • FileReaderURL.createObjectURL 是异步的,并且会在图像上传后失败,同时创建成功和错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多