Jquery Fileupload 设置上传图片的大小

Jquery Fileupload set the size of uploaded image

我正在项目中使用 jquery fileupload。我想知道是否有办法在上传后设置显示图像的大小。

比如它生成的代码是:

<span class="preview">
  <a href="/static/media/asd/espiro_gray.jpg" title="espiro_gray.jpg" download="espiro_gray.jpg" data-gallery=""><img src="/static/media/asd/espiro_gray.jpg"></a>
</span>

您可以使用 Jquery 文件上传的 'fileuploadadded' 事件并在其中添加图像大小调整代码。

试试这样的:

$(selector).bind('fileuploadadded', function (e, data) { 
// add your image resize code here
}

解决了以下问题:

// listen to the table for any new element
$('.table').bind("DOMNodeInserted", function(e){
   // get the new element
   var elem = e.target.nodeName;
   // if the element is 'TR' I continue
   if (elem == 'TR'){
      // there are two cases: 1) the image is 
      // added as preview: in this case 
      // there is not 'img' element generate. 
      // 2) the image is uploaded: in this case
      // we do generate a 'img' element, so I get
      // it with the following line
      var img = $(this).find("img");
      // undefined check
      if (img != undefined) {
         // set the right values
         img.attr('height', '100');
         img.attr('width', '100');
      }
    }
});

我希望这可以帮助其他人。

干杯。