2016-08-19 139 views
-2

我有一个包含图像的iframe。图像源链接到S3存储桶中的图像。我需要在Base64中编码此图像,并将iframe的主体保存在数据库中。将图像编码为Amazon S3的Base64

我该怎么做?

+1

的可能的复制[获取在JavaScript中的图像数据?](http://stackoverflow.com/questions/ 934012/get-image-data-in-javascript) –

+0

除了要求别人为你做你的工作之外,你还试过了什么?为什么它不起作用? –

回答

0

试试这个How to access the <img /> in document from iframe?

$('#Iframe1').contents().find('img').css({'width':'100%','height':'90%'}); 

,然后使用此功能从这里Get image data in JavaScript?

function getBase64Image(img) { 
    // Create an empty canvas element 
    var canvas = document.createElement("canvas"); 
    canvas.width = img.width; 
    canvas.height = img.height; 

    // Copy the image contents to the canvas 
    var ctx = canvas.getContext("2d"); 
    ctx.drawImage(img, 0, 0); 

    // Get the data-URL formatted image 
    // Firefox supports PNG and JPEG. You could check img.src to 
    // guess the original format, but be aware the using "image/jpg" 
    // will re-encode the image. 
    var dataURL = canvas.toDataURL("image/png"); 

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); 
} 
+0

非常感谢您的回答,但是当我尝试这样做时:var image = $('#Iframe1')。contents()。find('img'); (“Base64”+ current.getBase64Image(image));我有错误“提供的值不是一种HTMLImageElement” –

+0

注销图像的值,告诉我它是什么 –