简体   繁体   English

jQuery blueimp fileupload将画布图像预览附加到div

[英]jquery blueimp fileupload append canvas image preview to div

In my project I am using Jquery BlueImp fileUpload pluging for uploading images to my site and I need to display image preview. 在我的项目中,我正在使用Jquery BlueImp fileUpload插件将图像上传到我的网站,并且需要显示图像预览。 In example there is the following code: 在示例中,有以下代码:

node = $(data.context.children()[index]);
if (file.preview) {     
   node
      .prepend('<br>')
      .prepend(file.preview);    
   }

In this case file.preview is a preview image which is <canvas width="68" height="80"> . 在这种情况下, file.preview是一个预览图像,它是<canvas width="68" height="80"> So I need to get this canvas and 1st - change its dimentions for say 300x300px and 2nd - put it to some other div on my page. 因此,我需要获得此画布,并在第一个画布上将其尺寸更改为300x300px,然后在第二个画布上将其放入其他div The problem is that it can be only prepended to node as in example, so when I try to do something like: 问题是它只能像示例中那样放在node前面,所以当我尝试执行以下操作时:

 $('#uploaded_photo_p').prepend(file.preview);

no canvas is showed in my div. 我的div中没有​​显示画布。 Any ideas how to fix it would be welcome. 任何想法如何解决它都将受到欢迎。 Thank you. 谢谢。

UPD_1 If I change the dimentions of canvas like this: UPD_1如果我像这样更改画布的尺寸

$(file.preview)
      .attr("width", 136)
      .attr("height", 160);

the canvas width and height are changed, but the image is gone. 画布的宽度和高度已更改,但是图像消失了。 Any ideas how to keep an image in this case? 有什么想法在这种情况下如何保留图像吗? Thank you. 谢谢。

issue solved with this code: 此代码解决的问题:

 var canvas = data.files[0].preview;
 var dataURL = canvas.toDataURL();
 $("#uploaded_photo").css("background-image", 'url(' + dataURL +')')
                     .css('background-repeat', 'no-repeat')
                     .css('background-position', 'center center');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM